歸檔壓縮nginx
[root@client ~]# tar -zcf boot.tar.gz /boot/* &> /dev/null程序員
[root@client ~]# ls boot.tar.gz
boot.tar.gzshell
使用多個命令編程
[root@client ~]# ls ; who
anaconda-ks.cfg boot.tar.gz etc.tar.gz nginx-1.8.0 nginx-1.8.0.tar.gz
root tty1 2019-11-15 21:57
root pts/0 2019-11-15 22:04 (192.168.1.6)bash
要顯示美圓符,你必須在它前面放置一個反斜線。編程語言
[root@client ~]# echo "The cost of the item is \$15"
The cost of the item is $15ide
命令替換oop
[root@client ~]# testing=$(date)
[root@client ~]# echo $testing
2019年 11月 15日 星期五 22:15:57 CST
[root@client ~]# testing1=`date`
[root@client ~]# echo $testing1
2019年 11月 15日 星期五 22:16:19 CST測試
[root@client ~]# date +%y%m%d
191115this
重定向。
輸出重定向
[root@client ~]# date > testing
[root@client ~]# ls testing
testing
輸入重定向
[root@client ~]# wc -l < /etc/passwd
23
[root@client ~]# cat << EOF
> SLDKF
> FDSJL
> EOF
SLDKF
FDSJL
[root@client ~]# sort < /etc/passwd
[root@client ~]# sort < /etc/passwd > passwd.bak
數字運算
[root@client ~]# expr 1 + 8
9
[root@client ~]# expr 1 + a
expr: 非整數參數
[root@client ~]# a=9
[root@client ~]# expr 8 + $a
17
[root@client ~]# echo $?
0
[root@client ~]# expr 1 + a
expr: 非整數參數
[root@client ~]# echo $?
2
[root@client ~]# a=90yu
[root@client ~]# expr 1 + $a
expr: 非整數參數
[root@client ~]# a=7.9
[root@client ~]# expr 1 + $a
expr: 非整數參數
能夠用來判斷變量的值是否爲整數。
[root@client ~]# expr 9 \/ 3 #要使用\進行轉義。
3
[root@client ~]# a=2
[root@client ~]# b=3
[root@client ~]# c=$(expr $a + $b)
[root@client ~]# echo $c
5
使用方括號
[root@client ~]# c=$[$a+$b]
[root@client ~]# echo $c
5
[root@client ~]# a=4
[root@client ~]# b=9
[root@client ~]# c=8
[root@client ~]# d=$[$a*($c-$b)] #中括號裏可使用小括號的。
[root@client ~]# echo $d
-4
浮點數解決方案
[root@client ~]# bc
bc 1.06.95
Copyright 1991-1994, 1997, 1998, 2000, 2004, 2006 Free Software Foundation, Inc.
This is free software with ABSOLUTELY NO WARRANTY.
For details type `warranty'.
1+2
3
3/2
1
1.1+3
4.1
bc命令是能夠運算浮點數的。
[root@client ~]# bc -q
a=2
b=9
a+b
11
b/a
4
腳本中使用bc
[root@client ~]# var=$(echo "scale=4; 3.44/5"|bc) #保留4位小數。
[root@client ~]# echo $var
.6880
bc 命令能夠識別輸入重定向,容許你將一個文件重定向到bc命令來處理,但這一樣會叫人頭疼,由於你還得將表達式放到文件中。
test命令
[root@client script]# test 1 -eq 2 && echo "yes" || echo "no"
no
[root@client script]# if test
> then
> echo "yes"
> else
> echo "no"
> fi
no
[root@client script]# [ "$a" \> "$b" ] && echo "yes" || echo "no"
no
[root@client script]# [ "$a" \< "$b" ] && echo "yes" || echo "no" #比較符號須要轉義。r
yes
[root@client script]# char="abc"
[root@client script]# [ -z "$char" ] && echo "yes" || echo "no"
no
[root@client script]# [ -n "$char" ] && echo "yes" || echo "no"
yes
雙括命令容許你在比較的過程當中使用高等數學表達式,test命令只能使用簡單的算術操做,雙括號命令提供了更多的數學符號,這些符號對於用過其餘編程語言的程序員而言並不陌生,
if (( $var2 ** 2 > 90 ))
[root@client script]# [[ $USER == r* ]] && echo "root" || echo "no root"
root
[root@client script]# case $USER in
> root|ROOT) |在這裏是或的關係。
> echo "root"
> ;;
> esac
root
[ -d $HOME ] && [-w $HOME/testing ] #複合條件測試。
if (( $var11 ** 2 > 90 ))
[root@glusterfs01 scpripts]# vi badtest1.sh
#!/bin/bash
for test in I don't konw if this'll work
do
echo "word:$test"
done
[root@glusterfs01 scpripts]# bash badtest1.sh
word:I
word:dont konw if thisll
word:work
shell看到了列表值中的單引號並嘗試使用它們來定義一個單獨的數據值,這真是把事情搞得一團
[root@glusterfs01 scpripts]# vi badtest1.sh
#!/bin/bash
for test in I "don't" konw if "this'll" work
do
echo "word:$test"
done
[root@glusterfs01 scpripts]# vi badtest1.sh
#!/bin/bash
for test in I don\'t konw if this\'ll work
do
echo "word:$test"
done
[root@glusterfs01 scpripts]# bash badtest1.sh
word:I
word:don't
word:konw
word:if
word:this'll
word:work
for命令用空格來劃分列表中的每一個值,若是在單獨的數據值中有空格,就必須用雙引號將這些值圈起來。
[root@glusterfs01 scpripts]# vi badtest1.sh
#!/bin/bash
for test in "I don't" konw if "this'll work"
do
echo "word:$test"
done
[root@glusterfs01 scpripts]# bash badtest1.sh
word:I don't
word:konw
word:if
word:this'll work
從變量中讀取列表
[root@glusterfs01 scpripts]# vi test4
#!/bin/bash
list="Alabama Alaska Arizona Arkansas Colorado"
list=$list" Connecticut"
for state in $list
do
echo "Have you ever visited $state"
done
[root@glusterfs01 scpripts]# bash test4
Have you ever visited Alabama
Have you ever visited Alaska
Have you ever visited Arizona
Have you ever visited Arkansas
Have you ever visited Colorado
Have you ever visited Connecticut
從命令讀取值
[root@glusterfs01 scpripts]# vi states
Alabama
Alaska
Arizona
Arkansas
Colorado
Connecticut
Delaware
Florida
Georgia
[root@glusterfs01 scpripts]# for state in $(cat states)
> do
> echo "Visit beautiful $state"
> done
Visit beautiful Alabama
Visit beautiful Alaska
Visit beautiful Arizona
Visit beautiful Arkansas
Visit beautiful Colorado
Visit beautiful Connecticut
Visit beautiful Delaware
Visit beautiful Florida
Visit beautiful Georgia
更改字段分隔符
默認狀況下,bash shell會將下列字符看成分隔符
空格,製表符,換行符。
要想更改字段分隔符,更改IFS的值,使其只能識別換行符,那就必須這麼作。
[root@glusterfs01 scpripts]# vi test5b
#!/bin/bash
file="states"
IFS=$'\n'
for state in $(cat $file)
do
echo "Visit beautifull $state"
done
~
[root@glusterfs01 scpripts]# bash test5b
Visit beautifull Alabama
Visit beautifull Alaska
Visit beautifull Arizona
Visit beautifull Arkansas
Visit beautifull Colorado
Visit beautifull Connecticut
Visit beautifull Delaware
Visit beautifull Florida
Visit beautifull Georgia
用通配符讀取目錄
[root@glusterfs01 scpripts]# vi test6
#!/bin/bash
for file in /etc/*
do
if [ -d $file ]
then
echo -e "$file is \t\t a directory"
elif [ -f $file ]
then
echo "$file is a file"
fi
done
C語言風格的for命令
[root@glusterfs01 scpripts]# vi for5.sh
#!/bin/bash
for ((i=0; i<10; i++))
do
echo "The next number is $i"
done
[root@glusterfs01 scpripts]# bash for5.sh
The next number is 0
The next number is 1
The next number is 2
The next number is 3
The next number is 4
The next number is 5
The next number is 6
The next number is 7
The next number is 8
The next number is 9
使用多個變量
[root@glusterfs01 scpripts]# vi for6.sh
#!/bin/bash
for ((a=1, b=10; a<=10; a++, b--))
do
echo "$a-$b"
done
[root@glusterfs01 scpripts]# bash for6.sh
1-10
2-9
3-8
4-7
5-6
6-5
7-4
8-3
9-2
10-1
while命令
while命令某種意義上是if-then語句和for循環的混雜體,while命令容許定義一個要測試的命令,而後循環執行一組命令,只要定義的測試命令返回的是退出狀態碼0,它會在每次迭代的一開始測試test命令,在test命令返回非零退出狀態碼時。while命令會中止執行那組命令
[root@glusterfs01 scpripts]# vi while1.sh
#!/bin/bash
var=10
while [ $var -gt 0 ]
do
echo $var
var=$[ $var-1 ]
done
[root@glusterfs01 scpripts]# bash while1.sh
10
9
8
7
6
5
4
3
2
1
使用多個測試命令
[root@glusterfs01 scpripts]# vi while2.sh
#!/bin/bash
var1=10
while echo $var1 && [ $var1 -gt 0 ] #第一個測試簡單地顯示了var1變量的值,第二個測試用方括號來判斷var1變量的值。在循環內部,echo語句會顯示一條簡單的消息,說明循環被執行了,注意當你運行本例時輸出是如何結束的。
do
echo "This is inside the loop"
var1=$[ $var1-1 ]
done
[root@glusterfs01 scpripts]# bash while2.sh
10
This is inside the loop
9
This is inside the loop
8
This is inside the loop
7
This is inside the loop
6
This is inside the loop
5
This is inside the loop
4
This is inside the loop
3
This is inside the loop
2
This is inside the loop
1
This is inside the loop
0
until命令
和while命令工做的方式相反,until命令要求你指定一個一般返回非零退出狀態碼的測試命令,只有測試命令的退出狀態碼不爲0,bash shell纔會執行循環中列出的命令。
[root@glusterfs01 scpripts]# vi until.sh
#!/bin/bash
var1=100
until [ $var1 -eq 0 ]
do
echo "Inside the loop: $var1"
var1=$[ $var1-25 ]
done
[root@glusterfs01 scpripts]# bash until.sh
Inside the loop: 100
Inside the loop: 75
Inside the loop: 50
Inside the loop: 25
[root@glusterfs01 scpripts]# vi until.sh
#!/bin/bash
var1=100
until echo $var1 && [ $var1 -eq 0 ]
do
echo "Inside the loop: $var1"
var1=$[ $var1-25 ]
done
[root@glusterfs01 scpripts]# bash until.sh
100
Inside the loop: 100
75
Inside the loop: 75
50
Inside the loop: 50
25
Inside the loop: 25
0
嵌套循環
[root@glusterfs01 scpripts]# vi for10.sh
#!/bin/bash
for (( i=1; i<=5; i++ ))
do
echo "Starting loop $i"
for (( y=1; y<=5; y++ ))
do
echo " Inside loop: $y"
done
done
Starting loop 1
Inside loop: 1
Inside loop: 2
Inside loop: 3
Inside loop: 4
Inside loop: 5
Starting loop 2
Inside loop: 1
Inside loop: 2
Inside loop: 3
Inside loop: 4
Inside loop: 5
Starting loop 3
Inside loop: 1
Inside loop: 2
Inside loop: 3
Inside loop: 4
Inside loop: 5
Starting loop 4
Inside loop: 1
Inside loop: 2
Inside loop: 3
Inside loop: 4
Inside loop: 5
Starting loop 5
Inside loop: 1
Inside loop: 2
Inside loop: 3
Inside loop: 4
Inside loop: 5
[root@glusterfs01 scpripts]# vi for10.sh
#!/bin/bash
var1=3
while [ $var1 -ge 0 ]
do
echo "Starting loop $var1"
for (( y=1; y<=3; y++ ))
do
echo " Inside loop: $y"
done
var1=$[ $var1 -1 ]
done
[root@glusterfs01 scpripts]# bash for10.sh
Starting loop 3
Inside loop: 1
Inside loop: 2
Inside loop: 3
Starting loop 2
Inside loop: 1
Inside loop: 2
Inside loop: 3
Starting loop 1
Inside loop: 1
Inside loop: 2
Inside loop: 3
Starting loop 0
Inside loop: 1
Inside loop: 2
Inside loop: 3
[root@glusterfs01 scpripts]# vi for10.sh
doq
#!/bin/bash
var1=3
until [ $var1 -eq 0 ]
do
echo "Starting loop $var1"
for (( y=1; y<=3; y++ ))
do
echo " Inside loop: $y"
done
var1=$[ $var1 -1 ]
done
[root@glusterfs01 scpripts]# bash for10.sh
Starting loop 3
Inside loop: 1
Inside loop: 2
Inside loop: 3
Starting loop 2
Inside loop: 1
Inside loop: 2
Inside loop: 3
Starting loop 1
Inside loop: 1
Inside loop: 2
Inside loop: 3
循環處理文件
[root@glusterfs01 scpripts]# vi forfile.sh
#!/bin/bash
IFS_OLD=$IFS
IFS=$'\n'
for entry in $(cat /etc/passwd)
do
echo "Values in $entry -"
IFS=:
for value in $entry
do
echo " $value"
done
done
[root@glusterfs01 scpripts]# bash forfile.sh
Values in root:x:0:0:root:/root:/bin/bash -
root
x
0
0
root
/root
/bin/bash
Values in bin:x:1:1:bin:/bin:/sbin/nologin -
bin
x
1
1
bin
/bin
/sbin/nologin
控制循環
break命令是退出循環的一個簡單方法,能夠用break命令來退出任意類型的循環,包括while和until循環。
跳出單個循環
[root@glusterfs01 scpripts]# vi test17.sh
#!/bin/bash
for var1 in 1 2 3 4 5 6 7 8 9 10
do
if [ $var1 -eq 5 ]
then
break
fi
echo "Iteration number: $var1"
done
echo "The for loop is completed"
[root@glusterfs01 scpripts]# bash test17.sh
Iteration number: 1
Iteration number: 2
Iteration number: 3
Iteration number: 4
The for loop is completed
[root@glusterfs01 scpripts]# vi test17.sh
#!/bin/bash
var1=10
while [ $var1 -le 10 ]
do
if [ $var1 -eq 5 ]
then
break
fi
echo "Iteration number: $var1"
var1=$[ $var1-1 ]
done
echo "The for loop is completed"
~
[root@glusterfs01 scpripts]# bash test17.sh
Iteration number: 10
Iteration number: 9
Iteration number: 8
Iteration number: 7
Iteration number: 6
The for loop is completed
跳出內部循環
在處理多個循環時,break 命令會自動終止你所在的最內層的循環。
[root@glusterfs01 scpripts]# vi test20.sh
#!/bin/bash
for ((a=1; a<4; a++))
do
echo "Outer loop: $a"
for ((b=1; b<100; b++))
do
if [ $b -eq 5 ]
then
break
fi
echo " Inner loop: $b"
done
done
[root@glusterfs01 scpripts]# bash test20.sh
Outer loop: 1
Inner loop: 1
Inner loop: 2
Inner loop: 3
Inner loop: 4
Outer loop: 2
Inner loop: 1
Inner loop: 2
Inner loop: 3
Inner loop: 4
Outer loop: 3
Inner loop: 1
Inner loop: 2
Inner loop: 3
Inner loop: 4
跳出外部循環
break n 其中n指定了要跳出的循環層級,默認狀況下,n爲1,代表跳出的是當前的循環,若是你將n設爲2,break命令就會中止下一級的外部循環。
[root@glusterfs01 scpripts]# vi test21.sh
#!/bin/bash
for ((a=1; a<4; a++))
do
echo "Outer loop: $a"
for ((b=1; b<100; b++))
do
if [ $b -gt 4 ]
then
break 2
fi
echo " Inner loop: $b"
done
done
~
[root@glusterfs01 scpripts]# bash test21.sh
Outer loop: 1
Inner loop: 1
Inner loop: 2
Inner loop: 3
Inner loop: 4
continue命令
能夠提早停止某次循環中的命令,但並不會徹底終止整個循環,能夠在循環內部設置shell不執行的命令條件,這裏有個在for循環中使用continue命令的簡單例子。
[root@glusterfs01 scpripts]# vi test25.sh
#!/bin/bash
for ((var1=1; var1<15; var1++))
do
if [ $var1 -gt 5 ] && [ $var1 -lt 10 ]
then
continue
fi
echo "Iteration number: $var1"
done
[root@glusterfs01 scpripts]# bash test25.sh Iteration number: 1Iteration number: 2Iteration number: 3Iteration number: 4Iteration number: 5Iteration number: 10Iteration number: 11Iteration number: 12Iteration number: 13Iteration number: 14