1. 程式人生 > 其它 >1小時學會Shell Scripting

1小時學會Shell Scripting

技術標籤:linux運維

1小時學會Shell Scripting
最近學了下shell.這個教程很緊湊全面.這裡上傳程式碼

#!/bin/bash
# 空格問題
# 變數賦值兩邊無空格
# 條件測試兩邊均空格
# 條件測試字串比較 比較符號兩端空格 [ $gender = female ]
# expr [[]]
# Comment 中間看不懂的,請先看單雙引號的意思
echo "Hello World"

# define variable
myName="Qinning"
declare -r NUM1=5
num2=4

echo ---------operator{
}--------- # 1.修飾變數,給變數賦預設值 # 2.${var%pattern}, ${var%%pattern}, ${var#pattern}, ${var##pattern}, # ${var//dog/cat} # 3.列舉 touch {1,2,3}_file; touch {1..5}_file echo ---------operator'(())'用於算數運算比較--------- # operate (()) num3=$((NUM1%num2)) # 雙括號前面帶$ 則獲得表示式值,賦值給左邊變數 echo "5 % 4 = $num3" a=1 ((
a++))
a=$((a+1,a--,(a+1)**10)) # (())中可以逗號. 依次計算,取最後一個值 echo $a rand=5 let rand+=4 echo hello, $rand echo "rand++ = $((rand++))" echo "++rand = $((++rand))" echo "rand-- = $((rand--))" echo "--rand = $((--rand))" echo ---------logic--------- for((i=0;i<=5;i++
))
do echo $((i**2<10?i**2:10)) # 三目 done ((i<2)) || ((i>3)) && echo '成功執行||和&&' # &&正確則執行,||錯誤則執行 if ((((i%3))==0)) then echo "$i是3的倍數" fi num1=0 while [ $num1 -le '20' ] do if (((($num1%3))==0)) then echo $num1 fi ((num1++)) done until [ $num1 -le 5 ] do num1=$(($num1-4)) echo $num1 done echo ---------'""',"''",'``', '$(())'--------- # 雙引號,解析變數,解析`命令Or變數`,$(命令Or變數) echo "$i>3" echo '$i>3' # 單引號,純字串 echo $i>3 # 將i值寫入了3檔案 echo $((i>3)) # 1 $(()) echo `expr $i>3` # expr echo `echo 反引號` echo $(echo '$()') #$()新開一個shell執行,同時,命令替換隻替換標準輸出,去除錯誤輸出 echo `seq 3`;echo "`seq 3`" echo --------shell special variable -------- echo '$#'"=$#" # Shell 引數個數 echo '$$'"=$$" # Shell PID echo '$!'"=$!" # Shell most recent background command echo '$?'"=$?" # Shell 返回值 for p in $* do echo $p done echo --------shell function-------- getDate(){ date return } getDate getSum(){ local num1=$1 local Num2=$2 local sum=$((num1+Num2)) echo $sum } sum=$(getSum NUM1 num2) echo "$NUM1+$num2=$sum" echo ---------read以及operator[[]] 用於字串比較--------- # [[]]是[]的增強版本 # read -p "What's your age?" age if [ "${age:=15}" -ge '16' ] # []內變數要加上"" [[]]則不用 then echo "You can fuck." elif [ "$age" -eq '15' ] then echo "You can fuck next year." else echo "You can't fuck." fi # [ -d samp_dir ] || mkdir samp_dir str1="" str2="lugubrious" str3="jubilant" if [ "$str1" ] then echo "$str1 is not null" fi if [ -z "$str1" ] then echo "str1 has no value" fi if [ "str2" == "str3" ] then echo "$str2 equals $str3" elif [ "str2" != "str3" ] then echo "$str2 is not equal to $str3" fi if [ "str2" > "str3" ] then echo "$str2 is greater than $str3" elif [ "str2" <= "str3" ] then echo "$str2 is less than $str3" fi file1="./test_file1" file2="./test_file2" touch $file1 && chmod u+x $file1 mkdir $file2 if [ -e "$file1" ] then echo "$file1 exists" fi if [ -f "$file1" ] then echo "$file1 is a normal file" fi if [ -r "$file1" ] then echo "$file1 is readable" fi if [ -w "$file1" ] then echo "$file1 is witable" fi if [ -x "$file1" ] then echo "$file1 is executable" fi if [ -d "$file2" ] then echo "$file2 is a directory" fi echo --------regular expression-------- # read -p "Validate Date : " date date1=12121212 pat="^[0-9]{8}$" if [[ $date1 =~ $pat ]] then echo "$date1 is valid" else echo "$date1 is not valid" fi OIFS="$IFS" IFS="," # read -p "Enter 2 Numbers to add separated by a comma : " num1 num2 num1=1 num1=${num1//[[:blank:]]/} num2=${num2//[[:blank:]]/} sum=$((num1+num2)) echo "$num1+$num2=$sum" IFS="$OIFS" # read -sp "Enter the secret code" secret secret=password if [[ "$secret" == "password" ]] then echo Enter else echo Wrong Password fi echo --------case option很奇怪,記得字尾,記得括號-------- case ${age:+2} in [0-4]) echo "Too young for school" ;; 5) echo "Go to Kindergarten" ;; [6-9]|1[0-8]) grade=$((age-5)) echo "Go to grade $grade" ;; *) echo "You are too old for school" esac can_vote=0 ((age>=18?(can_vote=1):(can_vote=0))) echo --------string-------- rand_str="A random string" echo "String Length: ${#rand_str}" echo "${rand_str:2}" echo "${rand_str:2:8}" echo "${rand_str#*A}" echo --------shell call python-------- num7=1.2 num8=3.4 num9=$(python -c "print($num7+$num8)") echo $num9 cat<<END This text prints on many lines END echo $num7 $num8 $num9>test_file1 echo --------shell reading from files/arguments-------- while read n1 n2 n3; do printf "n1:$n1\nn2:$n2\nn3:$n3\n" done < test_file1 echo --------shell array-------- fav_nums=(3.14 2.718 .57721 4.6692) echo "Pi: $fav_nums[0]" fav_nums[4]=1.618 fav_nums+=(1 7) for i in ${fav_nums[*]}; do echo $i done echo "Array Length: ${#fav_nums[@]}" echo "Index 2 Length: ${#fav_nums[3]}" sorted_nums=($(for i in "${fav_nums[@]}" do echo $i done | sort )) unset 'sorted_nums[1]' for i in ${sorted_nums[*]}; do echo $i done unset sorted_nums

結果

Hello World
---------operator{}---------
---------operator(())用於算數運算比較---------
5 % 4 = 1
1024
hello, 9
rand++ = 9
++rand = 11
rand-- = 11
--rand = 9
---------logic---------
0
1
4
9
10
10
成功執行||和&&
6是3的倍數
0
3
6
9
12
15
18
17
13
9
5
---------"",'',``, $(())---------
6>3
$i>3
1

反引號
$()
1 2 3
1
2
3
--------shell special variable --------
$#=0
$$=2938
$!=
$?=0
--------shell function--------
2021年 02月 05日 星期五 03:40:08 CST
5+4=9
---------read以及operator[[]] 用於字串比較---------
You can fuck next year.
str1 has no value
lugubrious is not equal to jubilant
lugubrious is greater than jubilant
./test_file1 exists
./test_file1 is a normal file
./test_file1 is readable
./test_file1 is witable
./test_file1 is executable
./test_file2 is a directory
--------regular expression--------
12121212 is valid
1+4=5
Enter
--------case option很奇怪,記得字尾,記得括號--------
Too young for school
--------string--------
String Length: 15
random string
random s
 random string
--------shell call python--------
4.6
This text 
prints on
many lines
--------shell reading from files/arguments--------
n1:1.2
n2:3.4
n3:4.6
--------shell array--------
Pi: 3.14[0]
3.14
2.718
.57721
4.6692
1.618
1
7
Array Length: 7
Index 2 Length: 6
1
2.718
3.14
4.6692
.57721
7
```Hello World
---------operator{}---------
---------operator(())用於算數運算比較---------
5 % 4 = 1
1024
hello, 9
rand++ = 9
++rand = 11
rand-- = 11
--rand = 9
---------logic---------
0
1
4
9
10
10
成功執行||和&&
6是3的倍數
0
3
6
9
12
15
18
17
13
9
5
---------"",'',``, $(())---------
6>3
$i>3
1

反引號
$()
1 2 3
1
2
3
--------shell special variable --------
$#=0
$$=2938
$!=
$?=0
--------shell function--------
2021年 02月 05日 星期五 03:40:08 CST
5+4=9
---------read以及operator[[]] 用於字串比較---------
You can fuck next year.
str1 has no value
lugubrious is not equal to jubilant
lugubrious is greater than jubilant
./test_file1 exists
./test_file1 is a normal file
./test_file1 is readable
./test_file1 is witable
./test_file1 is executable
./test_file2 is a directory
--------regular expression--------
12121212 is valid
1+4=5
Enter
--------case option很奇怪,記得字尾,記得括號--------
Too young for school
--------string--------
String Length: 15
random string
random s
 random string
--------shell call python--------
4.6
This text 
prints on
many lines
--------shell reading from files/arguments--------
n1:1.2
n2:3.4
n3:4.6
--------shell array--------
Pi: 3.14[0]
3.14
2.718
.57721
4.6692
1.618
1
7
Array Length: 7
Index 2 Length: 6
1
2.718
3.14
4.6692
.57721
7