Use of the most complete shell script statement syntax (super-detailed)

Blog Outline:

  • 1. Talk-to-Talk Script--read Statement
  • 2. Characters of tests in shell scripts
  • 3. Examples of judgment scripts
  • 4. Conditional Judgment - if Statement
  • 5. Conditional Judgment - case Statement
  • 6. Conditional Judgment - Function function combined with case statement
  • 7. Circular Judgment - while, until Statements
  • 8. Fixed loop - for...do...do statement
  • 9. Loop - cut, set with for statement
  • 10. Examples of other script types
  • 11. Tracking and debug of shell scripts

The more canonical a shell script is, the more often the first few lines of each shell script have the following commentary information:

  • The shell used by the script, such as/bin/bash;
  • Version information of the script;
  • Authors and contact information for scripts;
  • history of the script;
  • The more specific instructions in the script are issued using absolute paths;
  • Script execution is required to preannounce and set environment variables.

Say nothing but grammar cases directly:

1. Talk-to-Talk Script--read Statement

Demand 1:
first name and 2. last name, and finally displays on the screen the contents of "Your full name is:":

[root@localhost ~]# vim 1.sh   #Edit the script as follows

#!/bin/bash
echo -e "yong lai xian shi wen jian full name:\n"
read -p "qing shu ru fir filename:" firname
read -p "qing shu ru sec filename:" secname
echo -e "\nyour full name is ${firname}${secname}."
#Where'\n'after echo means line break
[root@localhost ~]# sh 1.sh      #Execute script
yong lai xian shi wen jian full name:

qing shu ru fir filename:lv           #Enter the beginning of the file name manually
qing shu ru sec filename:jian         #Manually enter end of filename

your full name is lvjian.     #It automatically combines the beginning with the end and outputs

Demand II:
Suppose I want to create three empty files (by touch). The beginning of the filename is determined by the current user input. Suppose the user inputs the filename well. Today's date is 2019/08/25. I want to create these files the day before, yesterday, today.

[root@localhost ~]# vim 2.sh     #Edit script

#!/bin/bash
echo -e "yi ci chuang jian san ge file.\n"
read -p "qing shu ru filename:" filename
filename=${filename:-file}
date1=$(date --date '1 days ago' +%Y%m%d)
date2=$(date --date '2 days ago' +%Y%m%d)
date3=$(date +%Y%m%d)
file1="${filename}${date1}"
file2="${filename}${date2}"
file3="${filename}${date3}"
touch "${file1}"
touch "${file2}"
touch "${file3}"
[root@localhost ~]# sh 2.sh   #Execute script
yi ci chuang jian san ge file.

qing shu ru filename:lv           #Enter the beginning of a custom file name

[root@localhost ~]# find /root -name "lv*"   #Check to see if the creation was successful
/root/lv20190825
/root/lv20190827
/root/lv20190826

Demand II:
If we want the user to enter two variables, then multiply the contents of the two variables and output the multiplied result.

[root@localhost ~]# vim 3.sh      #Edit script

#!/bin/bash
echo -e "\nzhe shi yi ge suan cheng fa de jiao ben:\n"
read -p "qing shu ru yi ge shu zi:" A
read -p "qing shu ru di er ge shu zi:" B
sum=`echo "scale=4; ${A} * ${B}" | bc`
echo -e "\n${A}x${B} ==> ${sum}."
[root@localhost ~]# sh 3.sh        #Execute script

zhe shi yi ge suan cheng fa de jiao ben:

qing shu ru yi ge shu zi:3        #Enter the first number
qing shu ru di er ge shu zi:4     #Enter the second number

3x4 ==> 12.                           #Output Results

2. Characters of tests in shell scripts

All of the above tests are run through test, and can be replaced by'[]'. Write the type of test and the specified name in'[]', but there must be spaces around the brackets.(The bracket'[]'is recommended)

3. Examples of judgment scripts

Demand 1:

  1. Does this file exist, or if it does not, gives a "Filename does not exist" message and interrupts the program;
  2. If this file exists, it is determined that it is a file or directory and the output is "Filename is regular file" or "Filename is directory"
  3. Judge what permissions an executor has on this file or directory and output permission data!
[root@localhost ~]# vim 4.sh     #Edit script

#!/bin/bash
echo "yong lai ce shi wen jian huo dirctory."
read -p "qing shu ru yi ge wen jian ming:" filename
test -z ${filename} && echo -e "\nqing shu ru yi ge filename." && exit 0
test ! -e ${filename} && echo "filename does not exitst." && exit 0
test -f ${filename} && filetype="file"
test -d ${filename} && filetype="directory"
test -r ${filename} && prem="read"
test -w ${filename} && prem="${prem}+write"
test -x ${filename} && prem="${prem}+exe"
echo -e "\nthis is a ${filetype},it's perm.. is ${prem}."
[root@localhost ~]# sh 4.sh      #Execute script
yong lai ce shi wen jian huo dirctory.
qing shu ru yi ge wen jian ming:/root    #Enter a directory name

this is a directory,it's perm.. is read+write+exe.      #Output after script execution
[root@localhost ~]# sh 4.sh     #Reexecute script
yong lai ce shi wen jian huo dirctory.
qing shu ru yi ge wen jian ming:/etc/passwd      #Enter a file

this is a file,it's perm.. is read+write.       #Output after script execution

Demand II:

1. When a program is executed, it will let the user enter Y or N.
2. If the user enters Y or y, OK will be displayed, continue.
3. If the user enters N or n, ON,interrupt will be displayed.
4. If it is not a character within Y/y/N/n, it will loop through the script until you exit manually or enter the correct value (with a slight change, you can change it to enter "Y" if you press Enter by default, and do your own research).

[root@localhost ~]# vim 5.sh     #Edit script

#!/bin/bash
while [ "${yn}" != "Y" -o "${yn}" != "y" -o "${yn}" != "N" -o "${yn}" != "n" ]
do
read -p "qing shu ru 'Y' or 'N':" yn
[ "${yn}" == "Y" -o "${yn}" == "y" -o "${yn}" == "" ] && echo -e "\nOK,continue." && exit 0
[ "${yn}" == "N" -o "${yn}" == "n" ] && echo -e "\nON,interrupt." && exit 0
done
[root@localhost ~]# sh 5.sh     #Here is a script executed multiple times to test if it meets the requirements
qing shu ru 'Y' or 'N':

OK,continue.
[root@localhost ~]# sh 5.sh 
qing shu ru 'Y' or 'N':y

OK,continue.
[root@localhost ~]# sh 5.sh
qing shu ru 'Y' or 'N':n

ON,interrupt.
[root@localhost ~]# sh 5.sh
qing shu ru 'Y' or 'N':u
qing shu ru 'Y' or 'N':i
qing shu ru 'Y' or 'N':N

ON,interrupt.

Demand 3:
1. What is the file name of the program?
2. How many parameters are there?
3. If the number of parameters is less than 2, inform the user that the number of parameters is too small
4. What are all the parameters?
5. What is the first parameter?
6. Why is the second parameter

[root@localhost ~]# vim 6.sh     #The editing script is as follows

#!/bin/bash
echo -e "\ncheng xu de wen jian ming shi ${0}"
echo -e "\nyi gong you $# ge can shu."
[ $# -lt 2 ] && echo "can shu tai shao le ." && exit 0
echo  "your whole parameter is ==> '$*'."
echo "the 1st parameter ${1}."
echo "the 2nd parameter ${2}."
[root@localhost ~]# sh 6.sh a b c     #Execute script

cheng xu de wen jian ming shi 6.sh

yi gong you 3 ge can shu.
your whole parameter is ==> 'a b c'.
the 1st parameter a.
the 2nd parameter b.
[root@localhost ~]# sh 6.sh a      #Execute the script again

cheng xu de wen jian ming shi 6.sh

yi gong you 1 ge can shu.
can shu tai shao le .
#In order not to embarrass myself, I used Pinyin above, so that I could understand more [covering my face].

Needs 4:
Check to see if the www/ftp/mail service is on and display the results visually

[root@localhost ~]# vim 11.sh 

#!/bin/bash
file="/dev/shm/a.txt"
netstat -anpt  > ${file}
awk -F : '{print $4}' ${file} | awk '{print $1}' | grep "80" &> /dev/null
if [ $? -eq 0 ]
        then
        echo -e "www service is up\n"
fi
awk '{print $4}' ${file} | egrep "20|21" &> /dev/null
if [ $? -eq 0 ]
        then
        echo -e "ftp service is up\n"
fi
awk '{print $4}' ${file} | grep "25" &> /dev/null
if [ $? -eq 0 ]
        then
        echo -e "mail service is up\n"
fi
[root@localhost ~]# sh 11.sh     #Execute script tests
mail service is up

[root@localhost ~]# systemctl start httpd    #Start the WW service and test again
[root@localhost ~]# sh 11.sh 
www service is up

mail service is up

Demand_:
We all know that the first paragraph after the script is $1 and the second is $2.... So can we offset it, assuming that the original $2 becomes $1?

[root@localhost ~]# vim 7.sh      #The editing script is as follows

#!/bin/bash
echo "total parameter number is ==> $#"
echo "your whole parameter is ==> $* "
shift
echo "total parameter number is ==> $#"
echo "your whole parameter is ==> $* "
shift 3
echo "total parameter number is ==> $#"
echo "your whole parameter is ==> $* "
#The default shift parameter above is an offset of one position, or you can specify an offset parameter, such as shift 3, which means three offsets backwards
[root@localhost ~]# sh 7.sh a b c    #Execute the script and append three parameters
total parameter number is ==> 3
your whole parameter is ==> a b c 
total parameter number is ==> 2
your whole parameter is ==> b c 
total parameter number is ==> 2
your whole parameter is ==> b c 
#As you can see from the output, the offset is cumulative and the first offset is 1 bit by default.
#The second offset is 3 bits, so you have actually offset 4 bits of the original parameter (because of accumulation)
#But there are only three parameters, so it loops the offset, so the result is b and c.

For the explanations of'$#','$*'in the above script, you can refer to the following explanations:

4. Conditional Judgment - if Statement

Demand 1:

1. When a program is executed, it will let the user enter Y or N.
2. If the user enters Y or Y or presses the Enter key directly, OK will be displayed, continue.
3. If the user enters N or n, ON,interrupt will be displayed.
4. If it is not a character within Y/y/N/n, the output will be "I don't know what your choice is"

[root@localhost ~]# vim 11.sh     #Scripting

#!/bin/bash
read -p "Please input (Y/N): " yn

Tags: Linux vim shell ftp less

Posted on Tue, 27 Aug 2019 10:17:45 -0700 by gtomescu