Shell notes 1



The process ends and the variable is destroyed
Ordinary variables are inaccessible in child processes. They have to be environment variables
Scope: global variables in the current script, super global environment variables, and local variables in functions
Export Varname = value declares and assigns an environment variable
Name = xqw define and assign a common variable export name, and then declare it as an environment variable
Local variable local Varname = Val
Location variable $1 $2... References the parameters of the script
Naming of variables: only numbers, letters, underscores, and cannot start with numbers; Cannot have the same name as an existing environment variable
$?    Execution status return value of the previous process (command executed)

Execute the program, there are two kinds of return
    Status $? If it is executed correctly, it returns 0. If it is an error, it returns 1 ~ 255 (1, 2127 reserved by the system)

When the script executes, start a new bash
    The command line is executed directly, which is equivalent to the child bash of the current bash, and will inherit the environment variables of the parent process
    Environment variables need to be set for scripts automatically executed by the system, because there is no upper level parent bash process during execution

Unset Varname destroy variable
export PATH=$PATH:/bin/go 

By default, the value of a variable in the shell is a string and cannot be directly arithmetically operated

Execute script

./ to add the path to the path

Judgment expression

[ expression ]
[[ expression ]]
test expression

-eq  -ne  -gt  -ge  -lt  -le 

[ $a -eq $b ]
echo $?

Logical judgment

Short circuit phenomenon: if a && B A is true, the following B will be executed. If a is false, B will not be executed
id user3 &> /dev/null && echo 'user3 is exists.'

Short circuit phenomenon: if a | | B A is true, the latter need not be judged. If a is false, the latter need to be executed
id root &> /dev/null || echo 'root not exists.' && echo 'root is exists.'

If the user does not exist, add the user?
    id user2 &> /dev/null || useradd user2 
    ! id user3 &> /dev/null && useradd user3 

If the file exceeds 100 lines, it is a large file?
[ `wc -l ./passwd | cut -d' ' -f1` -gt 100 ] && echo 'big file.' || echo 'small file.'

!id user1 && useradd user1 && echo 'user1' | passwd --stdin user1 || echo 'user1 exists.'

Exit exit the current process script  
Exit can define what the execution status result is; If it is not defined, the exit execution status of the previous command of exit is regarded as the execution status result of the whole script by default  
exit #


let C=$A+$B
C = ` expr $a + $B ` there should be spaces between operands and operators in the expr command arithmetic operation expression, and the command reference should be used

File test

-e  -f  -d  -r  -w  -x 
[ -e file ]
[ -x file ]

Script debugging

Bash - n checks the syntax without executing the script
Bash - x single step, showing each command executed

Note: if the exit code is not defined in the script, the status code of the last command executed is the exit status code of the whole script


Execute it once, $1 $2 $3... The value of the position variable moves forward one bit as a whole
Shift # move # bit forward

Special variable

$1  $2  
Number of $# parameters
$* [email protected] parameter list


1. Pass three parameters to the script. The first is an integer, the second is an arithmetic operator, and the third is an integer to display the calculation results,
It is required to retain two digit precision, such as. / 5 / 2
echo "scale=2;111/22" | bc
bc <<< "scale=2;111/22"

2. Pass three parameters to the script, all of which are user names. After extracting the account information of these users, place them in the / TMP / testusers.txt file, and require the line number at the beginning of each line;

3. Judge the CPU manufacturer of the current host, and its information is vendor in the / proc / cpuinfo file_ In the ID line,
If the manufacturer is authenticamd, it will be displayed as AMD company;
If the manufacturer is genuineintern, it will be displayed as Intel company;
Otherwise, it is said to be a non mainstream company;
cputype=`grep 'vendor_id' /proc/cpuinfo | head -1 | cut -d':' -f2 | sed -r '[email protected][[:space:]][email protected]@g'`

case $cputype in
    echo "AMD."
    echo "intel."
    ehco "is not popular company.";;

4. Pass three integers to the script, judge the maximum number and the minimum number, and display them;

5. Write a script and add 5 users, user1 ~ user5. The password of each user is the same as the user name. After the user is added, it will be displayed that the user is successfully added
useradd user1 &> /dev/null && echo user1 | passwd --stdin user1 &> /dev/null && echo 'user1 add success.' || echo 'user1 exists.'

for i in {1..5}; do
    if id user$i &> /dev/null; then
        echo "user$i exists."
        useradd user$i
        echo "user$i" | passwd --stdin user$i &> /dev/null
        echo "add user$i success."    

6. To add three users, first judge whether they exist or not, and then add them. After adding, the total number of users added will be displayed. Finally, the total number of users in the current system will be displayed?

7. Given a user, if its uid is 0, it will be displayed as an administrator, otherwise it will be displayed as an ordinary user

8. Delete a user and delete their home directory together. The deletion is completed

9. Judge whether there are users on the current system whose default shell is bash, and if so, display the number of such users; Otherwise, it shows that there are no such users;

10. Judge whether there is a user whose default shell is bash on the current system; If yes, display the user name of one of them; Otherwise, it shows that there are no such users;
11. Given a file, judge whether there are blank lines in the file. If so, the number of blank lines will be displayed; otherwise, no blank lines will be displayed;
12. Given a user, judge whether its uid and GID are the same. If they are the same, the user will be displayed as "good guy". Otherwise, the user will be displayed as "bad guy". Further, it is required to obtain its ID number without using the ID command
13. Pass two parameters to display the product of the two, and the sum of the two parameters?
14. Without using the ID command, judge whether the user exists and whether its uid and GID are the same
if ! grep "^$USERNAME\>" /etc/passwd &> /dev/null; then
  echo "No such user: $USERNAME."
  exit 1

USERID=`grep "^$USERNAME\>" /etc/passwd | cut -d: -f3`
GROUPID=`grep "^$USERNAME\>" /etc/passwd | cut -d: -f4`
if [ $USERID -eq $GROUPID ]; then
  echo "Good guy."
  echo "Bad guy."

This work adoptsCC agreement, reprint must indicate the author and the link to this article

June wind

Recommended Today

New childhood fairy tale [139] little prince 32 — I planted this tree

Pain makes people sober. Everything in life originally depends on themselves. The mercy of others can’t win a better future. A petite thief said, “this time… I planted the tree and this time… I opened the road. If you want to buy the road money this time… ························································································· Another tall thief who blocked the way […]