Shell Programming Foundation

Keywords: Linux Java vim shell Programming

This article mainly writes some basic knowledge of shell scripts, programming specifications.

The first shell script

[root@localhost ~]# vim first.sh
#!/bin/bash
# This is first Shell Script !
cd /boot/
echo "The current path:"
pwd
echo "with vml File information for the beginning:"
ls -lh vml*

Execution script

source

[root@localhost ~]# source first.sh 
//The current path:
/boot
//File information starting with vml:
-rwxr-xr-x. 1 root root 5.7M Sep  4 14:02 vmlinuz-0-rescue-ec132d04a74d4b7e828b3905a6b83437
-rwxr-xr-x. 1 root root 5.7M Aug 23  2017 vmlinuz-3.10.0-693.el7.x86_64
[root@localhost boot]# 

.

[root@localhost ~]# . first.sh 
//The current path:
/boot
//File information starting with vml:
-rwxr-xr-x. 1 root root 5.7M Sep  4 14:02 vmlinuz-0-rescue-ec132d04a74d4b7e828b3905a6b83437
-rwxr-xr-x. 1 root root 5.7M Aug 23  2017 vmlinuz-3.10.0-693.el7.x86_64
[root@localhost boot]# 

sh

[root@localhost ~]# sh first.sh 
//The current path:
/boot
//File information starting with vml:
-rwxr-xr-x. 1 root root 5.7M Sep  4 14:02 vmlinuz-0-rescue-ec132d04a74d4b7e828b3905a6b83437
-rwxr-xr-x. 1 root root 5.7M Aug 23  2017 vmlinuz-3.10.0-693.el7.x86_64
[root@localhost ~]# 

./

[root@localhost ~]# ./first.sh
bash: ./first.sh: Permission denied
[root@localhost ~]# ls -lh first.sh 
-rw-r--r--. 1 root root 139 Sep 23 22:04 first.sh
[root@localhost ~]# chmod +x first.sh 
[root@localhost ~]# ls -lh first.sh 
-rwxr-xr-x. 1 root root 139 Sep 23 22:04 first.sh
[root@localhost ~]# ./first.sh 
//The current path:
/boot
//File information starting with vml:
-rwxr-xr-x. 1 root root 5.7M Sep  4 14:02 vmlinuz-0-rescue-ec132d04a74d4b7e828b3905a6b83437
-rwxr-xr-x. 1 root root 5.7M Aug 23  2017 vmlinuz-3.10.0-693.el7.x86_64

Symbol usage

Pipeline symbols and awk

[root@localhost ~]# grep "/bin/bash$" /etc/passwd | awk -F: '{print $1,$7}'
root /bin/bash
[root@localhost ~]# grep "/bin/bash$" /etc/passwd | awk -F":" '{print $1,$7}'
root /bin/bash
[root@localhost ~]# df -hT | grep "/$" | awk '{print $1,$6}'
/dev/sda2 21%
[root@localhost ~]# df -hT | grep "/$" | awk -F" " '{print $1,$6}'
/dev/sda2 21%

redirect output

[root@localhost ~]# echo "123" > test.txt
[root@localhost ~]# cat test.txt 
123
[root@localhost ~]# echo "456" >> test.txt 
[root@localhost ~]# cat test.txt 
123
456

redirect input

[root@localhost ~]# rm test.txt 
rm: remove regular file 'test.txt'? ^C
[root@localhost ~]# echo "y" > temp.txt
[root@localhost ~]# ls te*
temp.txt  test.txt
[root@localhost ~]# rm test.txt < temp.txt 
rm: remove regular file 'test.txt'? [root@localhost ~]# 
[root@localhost ~]# ls te*
temp.txt

Error redirection

  • Hybrid output
[root@localhost ~]# tar zcvf opt.tar.gz /opt/ &> error.log 
[root@localhost ~]# cat error.log 
tar: Removing leading `/' from member names
/opt/
/opt/rh/
  • Standard error output
[root@localhost ~]# tar zcvf opt.tar.gz /opt/ 2> error.log 
/opt/
/opt/rh/
[root@localhost ~]# cat error.log 
tar: Removing leading `/' from member names
[root@localhost ~]# tar zcvf opt.tar.gz /opt/ 2>> error.log 
/opt/
/opt/rh/
[root@localhost ~]# cat error.log 
tar: Removing leading `/' from member names
tar: Removing leading `/' from member names

variable

Define and view variables

[root@localhost ~]# Product=java
[root@localhost ~]# Version=1.8
[root@localhost ~]# echo $Product 
java
[root@localhost ~]# echo $Product $Version 
java 1.8

Braces

[root@localhost ~]# echo $Product1.8
.8
[root@localhost ~]# echo ${Product}1.8
java1.8

Quotation marks

[root@localhost ~]# echo "java $Version"
java 1.8
[root@localhost ~]# echo 'java $Version'
java $Version

Anti-apostrophe

[root@localhost ~]# which cat
/usr/bin/cat
[root@localhost ~]# ls -lh /usr/bin/cat
-rwxr-xr-x. 1 root root 53K Nov  6  2016 /usr/bin/cat
[root@localhost ~]# ls -lh `which cat`
-rwxr-xr-x. 1 root root 53K Nov  6  2016 /usr/bin/cat

read

[root@localhost~]# read-p:"Please enter an integer:" num
 Please enter an integer: 10
[root@localhost ~]# echo $num 
10

Range of action of variables

  • Setting local variables to global variables
[root@localhost ~]# echo $Product $Version 
java 1.8
[root@localhost ~]# bash
[root@localhost ~]# echo $Product $Version

[root@localhost ~]# exit
[root@localhost ~]# export Product Version
[root@localhost ~]# bash
[root@localhost ~]# echo $Product $Version
java 1.8
  • Direct definition of global variables
[root@localhost ~]# export num=100
[root@localhost ~]# echo $num
100
[root@localhost ~]# bash
[root@localhost ~]# echo $num 
100

The operation of numerical variables

[root@localhost ~]# num1=8
[root@localhost ~]# num2=5
[root@localhost ~]# expr $num1 + $num2
13
[root@localhost ~]# expr $num1 - $num2
3
[root@localhost ~]# expr $num1 \* $num2
40
[root@localhost ~]# expr $num1 / $num2
1
[root@localhost ~]# expr $num1 % $num2
3
  • Demo
[root@localhost ~]# vim suanshu.sh
#!/bin/bash
read -p "Please enter the first number:" num1
read -p "Please enter the second number:" num2
result1=`expr $num1 + $num2`
result2=`expr $num1 - $num2`
result3=`expr $num1 \* $num2`
result4=`expr $num1 / $num2`
result5=`expr $num1 % $num2`
echo "Addition: $num1 + $num2 = $result1"
echo "Subtraction: $num1 - $num2 = $result2"
echo "Multiplication: $num1 * $num2 = $result3"
echo "Division: $num1 / $num2 = $result4"
echo "Remaining: $num1 % $num2 = $result5"
[root@localhost ~]# chmod +x suanshu.sh 
[root@localhost ~]# ./suanshu.sh 
Please enter the first number: 8
 Please enter the second number:5
 Addition: 8 + 5 = 13
 Subtraction: 8 - 5 = 3
 Multiplication: 8 * 5 = 40
 Division: 8/5 = 1
 Balance: 8% 5 = 3

Special variables

environment variable

[root@localhost ~]# env

Location variable

[root@localhost ~]# vim suanshu.sh
#!/bin/bash
echo "The first position variable is: $1"
echo "The second position variable is: $2"
result1=`expr $1 + $2`
result2=`expr $1 - $2`
result3=`expr $1 \* $2`
result4=`expr $1 / $2`
result5=`expr $1 % $2`
echo "Addition: $1 + $2 = $result1"
echo "Subtraction: $1 - $2 = $result2"
echo "Multiplication: $1 * $2 = $result3"
echo "Division: $1 / $2 = $result4"
echo "Remaining: $1 % $2 = $result5"
[root@localhost ~]# chmod +x suanshu.sh 
[root@localhost ~]# ./suanshu.sh 8 5
 The first position variable is 8.
The second position variable is 5.
Addition: 8 + 5 = 13
 Subtraction: 8 - 5 = 3
 Multiplication: 8 * 5 = 40
 Division: 8/5 = 1
 Balance: 8% 5 = 3

Predefined variables

[root@localhost ~]# vim suanshu.sh
#!/bin/bash
echo "The first position variable is: $1"
echo "The second position variable is: $2"
result1=`expr $1 + $2`
result2=`expr $1 - $2`
result3=`expr $1 \* $2`
result4=`expr $1 / $2`
result5=`expr $1 % $2`
echo "Addition: $1 + $2 = $result1"
echo "Subtraction: $1 - $2 = $result2"
echo "Multiplication: $1 * $2 = $result3"
echo "Division: $1 / $2 = $result4"
echo "Remaining: $1 % $2 = $result5"
echo "Executed script name: $0"
echo "Number of parameters executed: $#"
echo "The parameters to be executed are as follows: $*"
[root@localhost ~]# chmod +x suanshu.sh 
[root@localhost ~]# ./suanshu.sh 8 5
 The first position variable is 8.
The second position variable is 5.
Addition: 8 + 5 = 13
 Subtraction: 8 - 5 = 3
 Multiplication: 8 * 5 = 40
 Division: 8/5 = 1
 Balance: 8% 5 = 3
 The name of the script to execute:. / suanshu.sh
 Number of parameters executed: 2
 Parametric content of execution: 8 5
[root@localhost ~]# echo $?
0

Posted by rossmurphy on Tue, 08 Oct 2019 20:09:34 -0700