| |

VerySource

 Forgot password?
 Register
Search
View: 942|Reply: 3

Help write 3 shell programs

[Copy link]

1

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-3-13 22:00:02
| Show all posts |Read mode
1. Write a SHELL program that accepts a number in the range of 2-15 as a parameter. Use the + sign,-sign, and vertical bar (|) to draw a square with the given parameter value as an edge on the standard output device. If the number of parameters on the command line is incorrect or the value is out of range, a corresponding error message is displayed.
    
  + ———— +
  | |
  | |
  + ———— +
2. Write a SHELL program so that it accepts only one command-line parameter. Use this parameter to enter a directory and calculate the total number of lines for all text files in that directory (excluding subdirectories).
For example, there are three text files 1.txt, 2.txt, and 3.txt in a certain directory, which are 3 lines, 20 lines, and 19 lines. Then the statistical result should be 42 lines.
If a command line parameter is entered as a file, an error message is displayed.

3. Write a Shell program to check whether the specified user is logged in to the system. If logged in, it will display "xxx user is logged in" and return 0, otherwise it will display "xxx user is logged in to the system" and return 1.
Reply

Use magic Report

0

Threads

3

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-8-19 17:45:01
| Show all posts
1.
echo "Please enter a number\c"
read size
len=`expr $size: ".*"`
result=0
if [$len = 1]
then
  result=`expr $size: "[2-9]"`
elif [$len = 2]
then
  result=`expr $size: "1[0-5]"`
fi
[$result = 0]&&{
  The number entered by echo must be between 2-15
  exit 1
}
let size=size-2
head='+'
line='|'
i=$size
sp=' '
while ["$i" -ne "0"]
do
  head="${head}-"
  line="${line} "
  let i=i-1
done
head="$head+"
line="$line|"
echo $head
while ["$size" -ne "0"]
do
  echo "$line"
  let size=size-1
done
echo $head
Reply

Use magic Report

0

Threads

3

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-8-19 18:00:01
| Show all posts
2.
([ $# = 0] || [! -D $1 ])&&{
  echo please enter a directory
  exit 1
}
lines=0
for fn in `ls $1/*.txt 2>/dev/null`
do
  line=`wc -l $fn|cut -c1-8`
  let lines=lines+line
done
echo "Number of lines in all text files: $lines"
Reply

Use magic Report

0

Threads

3

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-8-19 18:15:02
| Show all posts
3.
[$# = 0]&&{
  echo please enter a username
  exit 1
}
logins=`who|grep "^$1 "|wc -l|cut -c1-8`
["$longins" -gt "0"]&&{
   echo "$1 user has logged in"
   exit 0
}
while ["$logins" -eq "0"]
do
   logins=`who|grep "^$1 "|wc -l|cut -c1-8`
done
echo "$1 user is now logged in to the system"
exit 1


You can put it in the background during execution
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list