Linux Shell腳本基礎(一)

Linux Shell腳本基礎(一)

  1. 第一個shell腳本
#!/bin/bash
#This script is show now time and who login.
date
who

# 1.shell腳本說明:1行.指定使用shell 2行.註釋行 3行.date 4行.命令who

# 2.解決環境變量:PATH=¥PATH:/home/test或使用./test1(絕對路徑)

# 3.解決文件權限:chmod u+x test1

# 4.OK!實現完前三步就可以實現人生的第一個shell腳本了!(輸入:test1或./test1)
  • 顯示消息:echo -n 輸出在同行
  • 使用變量:$var
  • 命令替換:“反引號字符或$()
  • 重定向:輸入<,內聯輸入<<,輸入>,追加輸出>>
  • 管道連接(piping):將一個命令的輸出重定向到另一個命令中
  • 數學運算:expr命令,$[],浮點解決方案bc
  • 退出腳本:exit $?保存退出狀態碼(exit status)

    1. 使用結構和命令
  • 結構化命令(structured command):對shell腳本中的命令施加一些邏輯流程控制。
  • if-then語句
$ vi /home/test/test1.sh

#!/bin/bash
#testing the if statement
if pwd
then
    echo "It worked"
fi
  • if-then-else
$ vi /home/test/test2.sh

#!/bin/bash
#testing the if-then-else
testuser=nouser
if grep $testuser /etc/passwd
then
    echo "then bash files for user $testuser are:" is -a /home/$testuser/.b*
    echo
else
    echo "The user $testuser does not exist on this system."
    echo
fi
  • if嵌套語句
$ vi /home/test/test3.sh

#!/bin/bash
#testing the if-then-else
testuser=nouser
if grep $testuser /etc/passwd
then
    echo "The user $testuser exists on this system."
elif ls -d /home/$testuser/
then
    echo "The user $testuser not exists on this system."

    echo "But,$testuser has a directory."
else
    echo "The user $testuser does not exist on this system."
    echo "And,$testuser does not have a directory."
fi
  • test命令:測試命令退出狀態碼之外的條件。
// [codition]:方括號(左右有空格)定義了測試條件,作用等於test。

// 測試的條件:數值比較,字符串比較,文件比較。
  • 複合條件測試:&&(AND)、||(OR)
  • if-then高級特性:
// 雙括號(()):允許使用高級數學表達式

// 雙方括號[[]]:提供字符串比較。如:模式匹配
  • case語句
$ vi /home/test/test4.sh

#!/bin/bash
#testing case command
case $USER in
zld | zzz)
    echo "welcome,$USER"
    echo "Please enjoy your visit";;
testing)
    echo "Special testing account";;
*)
    echo "Sorry,not login";;
esac
  1. 循環語句
    • for 循環
// break跳出本層循環,break 123跳出所有循環
// continue繼續本層循環。

#!/bin/bash
for interface in $(ip link | grep ^[[:digit:]] | cut -d ':' -f 2 )
do
echo $interface"#######################"
ethtool -i $interface
echo "################################"
echo ""
done
  • while 循環
while condition
do
    command
done

//無限循環
while :
do
    command
done
  • until 循環
until condition
do
    command
done
  1. function 方法
[ function ] funname [()]

{

    action;

    [return int;]

}

// 1、可以帶function fun() 定義,也可以直接fun() 定義,不帶任何參數。

// 2、參數返回,可以顯示加:return 返回,如果不加,將以最後一條命令運行結果,作爲返回值。 return後跟數值n(0-255)
參數處理    說明
$#     傳遞到腳本的參數個數
$*     以一個單字符串顯示所有向腳本傳遞的參數。
如"$*"用「"」括起來的情況、以"$1 $2$n"的形式輸出所有參數。
$$    腳本運行的當前進程ID號
$!     後臺運行的最後一個進程的ID號
$@     與$*相同,但是使用時加引號,並在引號中返回每個參數。
如"$@"用「"」括起來的情況、以"$1" "$2""$n" 的形式輸出所有參數。
$-     顯示Shell使用的當前選項,與set命令功能相同。
$?     顯示最後命令的退出狀態。0表示沒有錯誤,其他任何值表明有錯誤。
#!/bin/bash

itisajoke(){

        echo "pid: $$"
        echo "params num:$#"
        echo "params:$@"
        echo $?
        echo $!

}
itisajoke -a aaa -b bbb haha joke
#!/bin/bash
echo "script name:$0"
echo "$# parameters"
echo "parameters@:$@"
for para in $@
do
        echo $para
        echo "####"
done
echo "parameters*:$*"
for para in $*
do
        echo $para
        echo "####"
done
#!/bin/bash

names=(liliang lihaidong mayuying)
age=(20.2223 19.454 18)

nameNum=${#names[@]}
ageNum=${#age[@]}

if [ $nameNum -ge  $ageNum ];then
        minNum=$ageNum
else
        minNum=$nameNum
fi

for i in $(seq 0 $(($minNum - 1)))
do

        printf "%-10s %-4.2f\n" "${names[$i]}" "${age[$i]}"

done


for ((i=0;i<$minNum;i=i+1))
do

        printf "%-10s %-4.2f\n" "${names[$i]}" "${age[$i]}"

done
  1. 調用外部文件
// 和其他語言一樣,Shell 也可以包含外部腳本。這樣可以很方便的封裝一些公用的代碼作爲一個獨立的文件。

Shell 文件包含的語法格式如下:

. filename   # 注意點號(.)和文件名中間有一空格source filename
// 創建兩個 shell 腳本文件。

// test1.sh 代碼如下:

#!/bin/bash
# author:菜鳥教程
# url:www.runoob.com

url="http://www.runoob.com"

// test2.sh 代碼如下:

#!/bin/bash
# author:菜鳥教程
# url:www.runoob.com

#使用 . 號來引用test1.sh 文件
. ./test1.sh

# 或者使用以下包含文件代碼
# source ./test1.sh

echo "菜鳥教程官網地址:$url"

接下來,我們爲 test2.sh 添加可執行權限並執行:

$ chmod +x test2.sh 
$ ./test2.sh 

參考於:http://blog.csdn.net/StandMyGround/article/details/52485697

發佈了68 篇原創文章 · 獲贊 119 · 訪問量 25萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章