shell指令碼基礎 六

2022-08-23 20:39:10 字數 1386 閱讀 1634

bash指令碼程式設計之與使用者互動

指令碼引數:指令碼傳遞數

使用者互動:通過鍵盤輸入資料,從而完成變數賦值操作

read變數賦值

[root@centos-7-24 opt]# read name

tom[root@centos-7-24 opt]#  echo $name

tom-p prompt  給個提示符

[root@centos-7-24 opt]# echo -n "enter a username"; read name

enter a username tom     

[root@centos-7-24 opt]# echo $name

tom[root@centos-7-24 opt]# read -p "enter a username:" name

enter a username:tom

[root@centos-7-24 opt]# echo $name

tom-t timeout 指定時間內未輸入將超時退出,變數賦值為空

[root@centos-7-24 opt]# [ -z "$name" ] && name="obama"   --變數name為空時賦值obama

[root@centos-7-24 opt]# echo $name

tom[root@centos-7-24 opt]# read -t 5 -p "enter a username:" name 5秒之後將超時

enter a username:tom

[root@centos-7-24 opt]# [ -z "$name" ] && name="obama"

[root@centos-7-24 opt]# echo $name

tom

#!/bin/bash

#read -p "enter a username:" name

[ -z $name ] && echo "a username is needed!!!" && exit 2

read -p "enter a passwd for $name,[password]:" passwd

[ -z $passwd ] && passwd="password"

ifid $name &>/dev/null

then

echo "$name is exists!!!"

else

useradd $name

echo "$passwd" |passwd $name --stdin

echo "add user $name is finished."

fibash -n 檢查指令碼語法錯誤

bash -x 顯示指令碼執行過程中錯誤的地方

shell指令碼基礎

執行shell指令碼有兩種方法 1 作為可執行程式 將上面的 儲存為 test.sh,並 cd 到相應目錄 chmod x test.sh 使指令碼具有執行許可權 test.sh 執行指令碼 注意,一定要寫成 test.sh,而不是 test.sh,執行其它二進位制的程式也一樣,直接寫 test.s...

shell指令碼基礎

shell定義 shell是命令解析器,將使用者的輸入的指令轉化為機器可以執行的程式。和c語言不同,指令碼有自己的語法。比較常用的格式是 bin bash或者 bin sh 如 這是乙個判斷輸入字元型別的程式 bin bash read key case in a z echo upperlette...

Shell指令碼基礎

1 shell是使用者與核心進行互動操作的一種介面,目前最流行的shell稱為bash shell 2 shell也是一門程式語言 解釋型的程式語言 即shell指令碼 3 乙個系統可以存在多個shell,可以通過cat etc shells命令檢視系統中安裝的shell,不同的shell可能支援的...