Shell Learning Notes

Keywords: shell Unix Linux Programming

Shell Learning Notes

Shell itself is a program written in C language. It is a bridge for users to use Unix/Linux. Most of the work of users is done through shell. Shell is both a command language and a programming language.

Shell can be executed in two ways:
- Interactive: Explain how to execute a user's command. When a user enters a command, the Shell executes a command.
- Batch processing: Users write a Shell script beforehand, which has many commands, so that the Shell can execute these commands at one time, without having to type one by one.

The first shell script:

  #!/bin/bash//Tell the system which Shell to use to execute it
    echo "hello world!"

When completed, save the file as. sh file (it is recommended to write in vim).
Step 2: You need to modify the permissions of files to be executable.

chmod +x ./***.sh  //Enter the location of the file and add executable permissions
,/***.sh    //Execution script
sh  ***.sh   //Execution script

The second script will explain the input:

   #!/bin/bash
    echo "what is you name?"
    read pname    //Read strings from stdin and assign them to pname using read command
    echo "hello,$pname"

Variables in the shell

(1) Definition of variables

pname="Zhang San"
url="www.baidu.com"
num=10

Variable names need not be added ($) when defining variables in shell scripts

(2) Use of variables
To use a defined variable, you need to precede the variable name.$

name="zhangsan"
echo $name

Sometimes {} is needed to distinguish variable names from strings.

echo "my name is ${name}."

(3) Read-only variables
The definition of read-only variables is supported in the shell.

age=14
readonly age

If the age is assigned later, the runtime will fail.

(4) deletion of variables

unset variable_name

Variables cannot be used again after they are deleted; however, the unset command cannot delete read-only variables.
When running the shell, there are three variables at the same time:
1) Local variables

Local variables are defined in scripts or commands, and are only valid in the current shell instance. Programs initiated by other shells cannot access local variables.
2) Environmental variables

All programs, including those launched by shell, can access environment variables. Some programs need environment variables to ensure their normal operation. Shell scripts can also define environment variables when necessary.
3) shell variables

Shell variables are special variables set by shell programs. Some of the shell variables are environment variables and some are local variables, which ensure the normal operation of the shell.

List of Special Variables
Variable Meaning
0 The file name n of the current script is passed to the parameters of the script or function. N is a number representing the number of parameters. For example, the first parameter is 1 and the second parameter is 2.
# Number of parameters passed to a script or function. * All parameters passed to a script or function.
@ All parameters passed to a script or function. When enclosed by double quotation marks (""), it is slightly different from * as will be discussed below.
The exit status of the last command or the return value of the function. Current Shell process ID. For Shell scripts, that's the process ID where these scripts are located.

shell array

bash supports one-dimensional arrays and does not limit the size of arrays. Distinguishing array elements with spaces

array_name(v1 v2 ...vn)

It can also be written in the following form:

array_name=(
v1
v2
v3
v4
)
array_name[0]=value0
array_name[1]=value1
array_name[2]=value2

Get the length of the array

The method of getting array length is the same as that of getting string length, for example:

# Gets the number of array elements
length=${#array_name[@]}
# perhaps
length=${#array_name[*]}
# Gets the length of a single element in an array
lengthn=${#array_name[n]}

shell if-else statement

Compared with C, if-else in shell is added then, the statement after that will be executed, and each if must have a fi to close, such as:

#!/bin/sh
a=10
b=20
if [ $a == $b ]
then
   echo "a is equal to b"
fi
if [ $a != $b ]
then
   echo "a is not equal to b"
fi

The same applies to if-else statements:

#!/bin/sh
a=10
b=20
if [ $a == $b ]
then
   echo "a is equal to b"
else
   echo "a is not equal to b"
fi

if... elif... fi statement

#!/bin/sh
a=10
b=20
if [ $a == $b ]
then
   echo "a is equal to b"
elif [ $a -gt $b ]
then
   echo "a is greater than b"
elif [ $a -lt $b ]
then
   echo "a is less than b"
else
   echo "None of the condition met"
fi

case-esac in shell

case... switch in esac and other languages. case statements are similar and are a multi-branch selection structure.
The usage is as follows:

echo 'Input a number between 1 to 4'
echo 'Your number is:\c'
read aNum
case $aNum in
    1)  echo 'You select 1'
    ;;
    2)  echo 'You select 2'
    ;;
    3)  echo 'You select 3'
    ;;
    4)  echo 'You select 4'
    ;;
    *)  echo 'You do not select a number between 1 to 4'
    ;;
esac   //andifStatements are similar and need to be usedesacStatement closes it

for loop in shell

The for loop format in the shell is:

for loop in 1 2 3 4 5
do
    echo "The value is: $loop"
done

Display files starting with. bash in the home directory:

#!/bin/bash
for FILE in $HOME/.bash*
do
   echo $FILE
done

Shell-while loop

COUNTER=0
while [ $COUNTER -lt 5 ]
do
    COUNTER='expr $COUNTER+1'
    echo $COUNTER
done

until loop in shell

#!/bin/bash
a=0
until [ ! $a -lt 10 ]
do
   echo $a
   a=`expr $a + 1`
done

Posted by agallo on Fri, 05 Apr 2019 17:48:29 -0700