Shell script syntax record

  Recently I was reading the book <<How tomcat works>>, which talked about how to start tomcat through shell scripts. Because I rarely write about this stuff, I record it here as my own study notes. At the same time, as your own technical accumulation, as a relatively advanced shell script developer, this article may not suit your taste. Please don't slap bricks.

   Let's start recording the body part of the shell script, which is an explanation of some basic commands.

   First a shell script is a text file that can be edited with an editor such as vi. To determine its licensing model, authorize the executable text file,

  $ chmod  +x scriptName
  $ chmod 755 scriptName

This will set read and write permissions for the file owner, and only read permissions for the group and others.

The script file can be executed in the following ways:

bash scriptName
sh scriptName
./scriptName

 Next are some commands commonly used in shell scripts to understand some commands in tomcat

 

  comment

 Use the # sign to indicate that the following text is ignored

# This is a comment

 It can also appear in the middle of a statement

echo Hello # print Hello

 

 clear

Use clear to clear the screen, the following statement clears the screen first, and then prints a message

clear
echo Shell scripts are useful

 

 exit

Use exit to exit the shell script. There are several exit states, 0 means normal exit, non-0 means abnormal exit. So when you encounter problems, you can use the following script to exit

exit 1

 

 echo

 Use the echo command to print a string of characters on the screen,

echo Hello World

 

  Calling A Function

You can use a period to call a function or call other shell scripts,

../test.sh

 

 System and User Defined Variables

Variable names must start with a alphanumeric or underscore. Use the equals sign to assign values ​​to variables,

myVar = Tootsie

Note: There cannot be blank characters before and after the equal sign, and it is necessary to note that variable names are case-sensitive. You can set the value of the variable to an empty string or leave the right side blank directly,

myVar=
myVar=""

 To access the value of a variable, use the variable name prefixed with a $ sign to access the variable,

echo $myVar

 Unix/Linux systems provide some system variables, such as HOME for the current user's home directory, PWD for the user's current directory, PATH for the path to find the calling command, etc.

 

  expr

Use expr to represent an expression, an expression must be enclosed in quotes,

sum = 'expr 100 + 200'
echo $sum

 It creates a variable named sum and assigns it the value 300,

Another example below:

echo 'expr 200 + 300'

 It prints something like 500 on the screen

The special 'uname' expression represents the name of the operating system,

echo 'uname'

 The special 'dirname filePath' returns the directory of the file, e.g. 'dirname /home/user/test.sh' returns /home/user

 

 Accessing Parameters

Just like passing arguments to functions, one can pass arguments to shell scripts. You can use $1 to represent the first parameter, $2 to represent the second parameter, and so on. $# command to get the number of parameters, $@ to get all parameters

 

shift

The Shift parameter moves the parameter back one place, $1 gets the value of $2, and $2 gets the value of $1

 

if...then....[else....] fi

The if statement block is used to test a condition and execute the appropriate command, the syntax is as follows:

if condition then

  list of commands

[else

  list of commands

]

be

Note that elif can be used instead of else if

The following example prints out Starting the application when a start parameter is obtained, and prints out Stropping the application when a stop is received,

if [ "$1" = "start" ]; then
  echo Starting the application
be
if [ "$1" = "stop" ]; then
 echo Stopping the application
be

 Note: In a condition, there must be a space after the [ and a space before the ]

When $1 is enclosed in double quotes, it will not generate an exception if no arguments are passed to it.

$0 represents the command for the user to execute the script, for example, if the following command is used to execute test.sh

./test.sh

$0 will then contail ./test.sh

$0 means ./test.sh

The following represent optional conditions

   

.  -f file,true if file exists
.  -r file,true if you have read access to file
. -z string,true if string is empty.
. -n string,true if striing is not empty
. string1 = string2,true if string1 equals string2
. string1 != string2, true if string is not equals to string2

 

for Loop

 The syntax of For loop is as follows,

 

for { var } in {list}
do
   list of commands
done
 E.g:

 

 

for i in 1 2 3
do
    echo iteration $i
done
 print out:

 

 

iteration 1
iteration 2
iteration 3
 

 

while Loop

 The syntax of the while loop is as follows:

 

while [condition]
do
   list of commands
done
 E.g

 

 

n=1
while [ $n -lt 3 ];
do
   echo iteration $n
   n=$((n+1))
done
 The output is

 

 

iteration 1
iteration 2
 -lt in [ $n -lt -3] means less than so it means the value of n is less than 3

 

 

case

Case runs a program you wrote to execute selectively. The syntax is as follows:

 

case $variable-name in
parrern1)
  list of commands
  ;;
pattern2)
  list of commands
;;
*)
  list of commands
;;
esac
 ;; The user finishes executing the command, *) indicates that it is executed when no other pattern matches

 

For example, the following script checks the name of the operating system. If you are using cgwin, OS400 or Linux, it will print out Operation the system not recogized

 

case "'uname'" in
CYGWIN*) echo cygwin;;
OS400*)  echo OS400;;
Linux*) echo Linux;;
*) echo Operating system not recogized
esac
 

 

 Output Redirection

Use > to locate the output into a file, for example, you can use a command like,

 

echo Hello > myFile.txt
 This file creates a file called myFile.txt and writes Hello to it. There will be no display on the screen.

 

Note: 1>&2 prints the error message on stdout to stderr and 2>&1 prints the output from stderr to stdout.

 

Conditional Executions

You can write commands or conditions to decide which command to execute. Use && and ||

 

command1 && command2
 Execute command2 if command1 returns an exit status of 0. Command1 can also use a conditional instead. If the condition is true, command2 will be executed, otherwise command2 will not be executed.

 

 

command1 || command2
 If the exit type of command1 is non-zero, execute command2.

 

 

command1 && command2 ||  command3
 If command1 returns 0 exit status, execute command2, otherwise execute command3

 

This record is complete! ! !

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326944378&siteId=291194637