Shell programming

Creating a shell script

Shell scripts are suffixed with.sh

Create a.sh file using vim

# vim run.sh
Copy the code

Write a script

The first line specifies which Shell the system uses

#! /bin/sh
Which shell to use for the first line of execution
Copy the code

Execute the script

Using the sh command

sh ./if.sh
Copy the code

Direct execution

To directly execute shell scripts, the sh file must have the executable permission

[root@localhost sh]# ./if.shBash:./if.sh: the permission is insufficientCopy the code

Example Add the executable permission

chmod +x if.sh
# or
chmod +1 if.sh
Copy the code
[root@localhost sh]# ./if.shBash:./if.sh: Permission not enough [root@localhost sh]# chmod +x if.sh 
[root@localhost sh]# ./if.sh
ss
Copy the code

Input redirection

0 The standard input is stdin

1 Standard output stdou

2 Standard error output stderr

Use the less-than sign < for input redirection

[root@localhost sh]# cat < /etc/fstab
# /etc/fstab
# Created by anaconda on Tue Apr 13 08:37:07 2021
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/mapper/rhel-root /    xfs  defaults 0 0
/dev/mapper/rhel-swap swap swap defaults  0 0
/dev/sdb1             /mnt ext3 defaults  0 1 
Copy the code

Output redirection

There are two types of output redirection

> Direct output

>> Append output

Direct output >

ls > la.txt
Copy the code

Overwrites the previous content of the file

[root@localhost sh]# ls > sh.txt
[root@localhost sh]# cat < sh.txt
case_isYN.sh
case.sh
dir
double_if.sh
Copy the code

Append >>

ls >> la.txt
Copy the code
[root@localhost sh]# ls > sh.txt
[root@localhost sh]# ls >> sh.txt
[root@localhost sh]# cat < sh.txt
case_isYN.sh
case.sh
dir
case_isYN.sh
case.sh
dir
Copy the code

/dev/null

/dev/null is a character special file, it belongs to the empty device, it is a special device file, it will discard any data written to it, the content written to it is lost forever, and there is nothing to read.

The pipe

Will be behind the output of the command as | | command input

[root@localhost sh]# ps -ef|grep java
root 6807 3423 0 11:47 pts/0 00:00:00 grep --color=auto java
Copy the code

Special characters

$represents variable substitution

\ Escape the character and let the shell treat it as a normal character

  1. Double quotes (“)

    All characters except $’ \ are used as normal characters

  2. Single quotes (‘)

    All as ordinary characters

  3. Inverted quotation marks (‘)

    The content in the back quotes is interpreted as a command line. The command line is executed first, and the output of the command line replaces the content in the back quotes

annotations

# note

#! /bin/sh
# The first line is not a comment
# I am a note
ps -ef
Copy the code

variable

Variable name = value

The variable name =

msg="hello shell"
age=
Copy the code

Use the variable

Use the $variable name

msg="hello shell"
echo $msg
Copy the code

System variables

$0 The name of the current program $n (n = 1, 2… N) the NTH argument to the current program method,n=1,2,3… 9 $* All parameters of the current program (excluding the program itself) $# Number of parameters of the current program (excluding the program itself) $? Status of a command or program after it is executed. 0 is returned, indicating that the command is successfully executed. $UID Current user ID $PWD Current directory