Linux programming experiment --shell

Experiment six: shell programming

 

Experimental Procedure

  1. log in system.

a. Using a user name and password to create a system test.

b. Open the Terminal application.

 

  1. Basics:

a. Understand the role shebang statement.

b. Grasp two ways to execute a shell script:

   name of the shell script name

   Executable script

c. For the following script, it was executed in two ways.

 

  1. Create a shell script service, complete the following functions:

a. Clear the screen.

b. Jump two lines.

c. Displays the current date and time.

d. Displays the current number of users.

e. Beep, and then displays the message "Now at your service".

answer:

clear

echo -e "\n\nDateTime:`date`"

echo -e "\nUser Number:`who | wc -l`"

echo -e "\07\nNow at you service"

 

  1. Create a shell script max, requirements are as follows:

a. User three numbers.

b. Obtain the maximum number of them and display.

answer:

if [ $1 -gt  $2 ]

then

   max=$1

else

   max=$2

be

if [ $max -lt $3 ]

then

   max=$3

be

echo "The max is: $max"

exit 0

 

  1. Create a shell script sum, requirements are as follows:

a. Use of: $ sum ab, where a and b represent two numbers, a <b.

b. Function from a to b is calculated and accumulated.

c. Displayed on the screen: a + ... + b = result. and the result is accumulated.

 

answer:

if [ $1 -ge $2 ]

then

   exit 1

be

a=$1

b=$2

sum=0

while [ $a -le $b ]

do

   sum=`expr $sum + $a`

   a=`expr $a + 1`

done

echo "$1+…+$2=$sum"

exit

 

  1. Create a shell script check, requirements are as follows:

a. Whether there .profile file, output test results to check the current home directory.

b. If there is no .profile file, create a .profile file.

c. And add the following two lines of information at the end of .profile:

echo “Hello, Welcome to Linux!”

echo “Current Date and Time: [`date`]”

 

answer:

cd

if [ ! -e .profile ]

then

   touch .profile

   echo "Not exist .profile"

else

   echo "Exist .profile"

be

echo 'echo "Hello, Welcome to Linux!"' >> .profile

echo 'echo "Current Date and Time:`date`"' >>.profile

echo  "Write Success!"

exit 0

Published 58 original articles · won praise 22 · views 9851

Guess you like

Origin blog.csdn.net/zsd0819qwq/article/details/103868889