天天看點

shell簡單學習Day1

1.shell腳本的種類和注意事項

種類:cat /etc/shells

bin/bash=======>Linux 預設使用

bin/sh

bin/csh

bin/ksh

注意事項:和C不同成功傳回0失敗傳回1

好的文法習慣:當去取變量的數值的時候一般加上"VAR"

2.執行腳本方法

1.chmod a+x shell.sh ./shell.sh

2./bin/bash shell.sh

3. . shell.sh

shell簡單學習Day1

3.指令和文法

A.變量

VAR=hello --本地變量(局部變量) echo $VAR env | grep &VAR 沒有效果

export VAR設定成環境變量(全局變量) echo $VAR || env | grep &VAR 有效果

alias ===将指令重命名

B.指令代換

反引号======ESC 下面的按鍵------>替換功能

VAR=

date

----------------------->echo $VAR ---->調用date的功能

( ) 等 價 : V A R = ()等價:VAR= ()等價:VAR=(date)

C.算術代換

VAR=33,echo $((VAR+3)) == echo ( ( (( ((VAR+3)) == echo $[ VAR+3]

echo $[ 2#10 +11]//2進制的10+(十進制)11

D.轉義字元:\

touch $\ \test.sh ---- > 生成 $ test.sh 删除也是

touch ./-abc -abc

等價touch – -abc

touch ./-------xyz -------xyz

F.單引号雙引号

VAR=

date

echo ' V A R ′ e c h o " VAR' echo " VAR′echo"VAR"

G.條件測試和比較符号

VAR=2

test $VAR -gt 1

比較符号

-gt 大于與否

-lt 小于

-qt大于

-eq等于

-ne不等于

-d是否是目錄

-f是否是普通目錄

-p特殊檔案

-z判斷字元串是0

-n不是0

H.與或非

-a為與 &&

-o 為 ||

-x為^

VAR=abc

F.分支語句

if [ -d t.txt ]; then

内容

fi

特殊

if :;then echo “always true” ; fi

***注意: 表示一直為真

測試代碼

#! /bin/bash
echo "Is it morning? please answer yes or no"
#相當于fgets()
read YES_OR_NO
if [ "$YES_OR_NO" = "yes" ]; then
	echo "Good morning !"
elif [ "$YES_OR_NO" = "no" ]; then
	echo "Bad weather!!"
else
	echo "Sorry, $YES_OR_NO not recognize. Enter yes or no"
fi

           
shell簡單學習Day1

case “$VAR” in

y] ;;

N] ;;

] ;;

;;---->代表break

read -p "please enther the first number n1 " n1

#! /bin/bash
echo "Is it morning? please answer yes or no"
#相當于fgets()
read YES_OR_NO
#取放到集合裡面IN
case "$YES_OR_NO" in
#注意隻有)2
yes|y|YES|Yes)
	echo "Good morning !";;
no|No)
	echo "Bad weather!!";;
*)
	echo "Sorry, $YES_OR_NO not recognize. Enter yes or no"
	return ;;
esac
           
shell簡單學習Day1

循環

foreach----周遊 for/do/done

for sth in [email protected] ;do

done

#! /bin/bash
echo "Enter Yes or No"

for FILENAME in $(ls); do
	printf "$FILENAME "
	if [ -f "$FILENAME" ];then
		echo " "$FILENAME" is a file"
	else
		echo "It is not a file"
	fi
done 
           

while []; do

done

#! /bin/bash

count=3
echo "Enter password"
read TRY
while [ "$TRY" != "secret" -a $count -gt 0 ]
do
	count=$[count-1]
	echo "Sorry ,try again"
	read TRY
	
done

           

read

read -p 讀取控制台輸入的值并賦予num1
read -p "please write a num " NUM1
echo "you write num is $NUM1"
-t是在規定時間為10秒内沒有輸入時就會直接執行下面的語句
read -t 10  -p "please write a num " NUM2
echo "the else num is $NUM2"
           

G.函數建構

#! /bin/bash
#一次建立多個目錄,目錄名字從參數中傳進取,腳本測試目錄是否存在,如果不存在,先列印不存在的資訊然後建立該目錄

is_directory()
{
	DIR_NAME=$1

	
		#從參數中判斷
		if [ ! -d $DIR_NAME ]; then
			#如果檔案存在則傳回
			return 1
		else
			return 0
		fi
}

#指令行參數調用

for DIR in "[email protected]"; do
	if is_directory "$DIR";then
		echo "file have already created "
	else
		echo "$DIR doesn't exist. Create it now..."

		#标準輸出重建到黑洞裡面不管建立成功還是失敗他都會重定向到黑洞裡面

		mkdir $DIR > /dev/null 2>&1
		#建立失敗則退出程式
		if [ $? -ne 0 ]; then
			echo "Can't create directory $DIR"
			exit 1
		fi
	fi

done


           

H.位置參數和特殊變量!!!

$0 擷取執行檔案名字

$n n取1-n 表示執行檔案傳入的參數

$# 擷取參數的總數量

$* 一行列印所有的參數

[email protected] 列印n行n個參數用于for 語句

$? 列印上次執行的結果

$$ 列印執行的腳本的PID

shift 将參數左邊移動

echo -n "hello\n"不解析轉義字元

echo -e “hello\n” 解析轉義字元

G.管道和檔案重定向

tee

ps aux | grep init | tee out ====>篩選出來的資訊存進out 的檔案裡面,并且顯示在螢幕上面

cmd > file

cmd >> file

cmd > file 2>&1 >後面沒有空格=>将輸出重定向為标準輸入=>内容給檔案

cmd < file >file2 === <将file的内容重定向到螢幕上面在重定向到file2裡面

wc -l test_if_case.sh > out

cat < test_if_case.sh > out

cmd < &fd ========>把檔案描述符當作标準輸入

cmd > &fd ========>把檔案描述符号當作标準輸出

cmd < &- 關閉标準輸出

繼續閱讀