1.判斷變數複製代碼 代碼如下:read -p "input a word :" wordif [ ! -n "$word" ] ;then echo "you have not input a word!"else echo "the word you input is $word"fi2.判斷輸入參數複製代碼 代碼如下:#!/bin/bashif [ ! -n "$1" ] ;then
在Windows下寫了一個shell指令碼,上傳到Linux下執行時報錯:複製代碼 代碼如下:[root@localhost test]# ./test.sh -bash: ./test.sh: /bin/sh^M: bad interpreter: No such file or directory test.sh源碼: 複製代碼 代碼如下:#!/bin/sh echo "test shell
出現bad interpreter:No such file or directory的原因,是檔案格式的問題。這個檔案是在Windows下編寫的。換行的方式與Unix不一樣,但是在vim下面如果不Set一下又完全看不出來。問題分析:1、將windows 下編寫好的SHELL檔案,傳到linux下執行,提示出錯。2、出錯資訊:bad interpreter:
在shell中,調用函數時可以向其傳遞參數。在函數體內部,通過 $n 的形式來擷取參數的值,例如,$1表示第一個參數,$2表示第二個參數...帶參數的函數樣本:#!/bin/bashfunWithParam(){ echo "The value of the first parameter is $1 !" echo "The value of the second parameter is $2 !" echo "The value of the tenth parameter is $
在迴圈過程中,有時候需要在未達到迴圈結束條件時強制跳出迴圈,Shell使用兩個命令來實現該功能:break和continue。break命令break命令允許跳出所有迴圈(終止執行後面的所有迴圈)。下面的例子中,指令碼進入死迴圈直至使用者輸入數字大於5。要跳出這個迴圈,返回到shell提示符下,需要使用break命令。 複製代碼 代碼如下:#!/bin/bashwhile :do echo -n "Input a number between 1 to 5:
echo是Shell的一個內部指令,用於在螢幕上列印出指定的字串。命令格式: 複製代碼 代碼如下:echo arg您可以使用echo實現更複雜的輸出格式控制。顯示逸出字元複製代碼 代碼如下:echo "\"It is a test\""結果將是:"It is a test"雙引號也可以省略。顯示變數複製代碼 代碼如下:name="OK"echo "$name It is a test"結果將是:OK It is a test同樣雙引號也可以省略。如果變數與其它字元相連的話,需要使用大括弧({ }