shell script學習筆記一

2021-08-29 09:01:30 字數 1768 閱讀 8460

#####the fist line should be the following#####

#!/bin/bash

#####return value#####

exit n

#####usage of read command,讀鍵盤的輸入#####

read [-pt] variable

-p: 後面可以接提示資訊

-t: 後面可以接等待的秒數,這樣指令碼不用一直等下去

例子:read -p "input your name: " name;

echo $name;

#3秒內輸入名字

read -p "input your name: " -t 3 name;

#####date command#####

#2 years/days/minutes/seconds ago

date1=`date --date='2 years/days/minutes/seconds ago' +%y%m%d`

echo $date1

#2 years/days/minutes/seconds next

date2=`date --date='-2 /days/minutes/seconds ago' +%y%m%d`

或者date2=`date --date='-2 /days/minutes/seconds next' +%y%m%d`

echo $date2

#####數值運算#####

total=$((運算表示式))

e.g. total=$((2+3))

#####判斷表示式#####

判斷表示式有test指令和。

##test指令

當我們要檢測系統上某個檔案是否存在或者屬性時,可以使用test指令。用&&判斷真值,||判斷假值。例如:

test -e ch04.sh && echo "exist" || echo "no exist"

意思就是當檔案ch04.sh存在時列印"exist",不存在時列印"no exist"。

#判斷字串長度是否為0

test [-zn] string

-z: 當string為空時,返回true

-n: 當string不為空時,返回true

test -z "" && echo "empty" || echo "not empty"

#empty

test -z " " && echo "empty" || echo "not empty"

#not empty

test -n "" && echo "not empty" || echo "empty"

#empty

test -n " " && echo "not empty" || echo "empty"

#not empty

#判斷字串是否相等,注意=和!=兩邊要留乙個空格

test str1 = str2

test str1 != str2

#邏輯與,或,非

-a(and)

-o(or)

!##判斷符號

注意點:

1. 在中括號內的各個元素必須有空格隔開

2. 在中括號內的變數,最好用雙引號括起來

3. 在中括號內的常數,最好用單引號或者雙引號括起來

例子:yn='y'

[ "$yn" == "y" -o "$yn" == "y" ] && echo "equal" || echo "not equal"

#equal

shell script學習筆記2

bash中的變數都是無型別的,可以通過declare來宣告int型變數 0,1,2 9,如果引數超過9個,那就需要用 的方式 引數數目 引數集合 最後乙個引數 整數比較 eq,ne,gt,ge,lt,le,如 if a eq b 需要雙括號,如 a b 字串比較 a z 如果 a以 z 開頭,那麼將...

shell script學習筆記三

迴圈 有四種方式 while do done,until do done,for do done,for in do done while do done while condition do do what you want done 解釋 當condition不成立時終止迴圈 until do ...

Linux學習筆記(Shell Script)

vim showname.sh bin bash program user inputs his first name and last name.program shows his full name.history 2015 07 16 vbird first release path bin ...