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

在這裏插入圖片描述
3.指令和語法
A.變量

VAR=hello --本地變量(局部變量) echo $VAR env | grep &VAR 沒有效果
export VAR設置成環境變量(全局變量) echo $VAR || env | grep &VAR 有效果
alias ===將指令重命名

B.命令代換

反引號======ESC 下面的按鍵------>替換功能
VAR=date ----------------------->echo $VAR ---->調用date的功能
()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 'VARecho"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

在這裏插入圖片描述

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

在這裏插入圖片描述

循環
foreach----遍歷 for/do/done
for sth in $@ ;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 "$@"; 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 表示執行文件傳入的參數
$# 獲取參數的總數量
$* 一行打印所有的參數
$@ 打印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 < &- 關閉標準輸出

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章