#!/bin/bash#for testfor var in A B C ;doecho"var is $var"done#for and if test#打印一些rpm包的統計信息for rpmpackage in"$@";doif[ -r "$rpmpackage"];thenecho"========$rpmpackage=========="
rpm -qi -p $rpmpackageelseecho"ERROR: cannot read file $rpmpackage"fidone#特殊變量$@,該變量包含有輸入的所有命令行參數值。如果你運行showrpm openssh.rpm w3m.rpm webgrep.rpm,#那麼 "$@"(有引號) 就包含有 3 個字符串,即openssh.rpm, w3m.rpm和 webgrep.rpm。#$*的意思是差不多的。但是隻有一個字串。如果不加引號,帶空格的參數會被截斷。# echo and regulation expressionecho *.sh #所有的以.sh結尾的文件echo"*.sh"#取消了正則表達式功能echo'*.sh'#取消了正則表達式功能echo$SHELL#常用環境變量,可以輸出正確的值echo"$SHELL"#常用環境變量,可以輸出正確的值
echo'$SHELL'#按字符串輸出
assign value ,‘if’,‘switch’ and ‘select’
#!/bin/bash# assign value
a="hello,world"#print printf'%s\n'"A is: $a"
num=3
echo"this is the $numnd"echo"this is the ${num}nd"echo"this is the {$num}nd"#shell變量默認都是字符串變量
var=1
var=$var+1
echo$var#變量算術的功能
var=0
#bash裏面可以用(())來執行C風格的算術表達式echo$((var+=1))echo$((var++))echo$((var=var*var))let'var=var/3'echo$((var+=2))
var=$((var-1))#if語句# if ...;then# ...#fiif["${SHELL}"=="/bin/bash"];thenecho"your login shell is the bash (bourne again shell)"elseecho"your login shell is not bash but ${SHELL}"fiif["${SHELL}"=="/bin/bash"];thenecho"your login shell is the bash (bourne again shell)"elseecho"your login shell is not bash but ${SHELL}"fi#&&和||[ -f "/etc/shadow"]&&echo"this is uses shadow passwords"#如果這是一個文件,則輸出後面的語句
mailfolder=/var/spool/mail/james
[ -r "$mailfolder"]||{echo"can not read $mailfolder";exit 1;}echo"$mailfolder has mail from:"grep"From "" $mailfolder
#判斷是否可讀,如果可讀打印以From開頭的行,如果不可讀或者或操作生效打印錯誤信息後腳本退出。
#要注意 Shell 中的 && || 程序流操作符不表現任何優先級區別,完全是先看到誰就先處理誰的關係。
#case語句
ftype="$(file"$1")"
case "$ftype" in
"$1:zip archive"*)
unzip "$1";;
"$1:gzip compressed"*)
gunzip "$1";;
"$1:bzip2 compressed"*)
bunzip2 "$1";;
*)
echo "File $1 can not be uncompressed with smartzip";;
esac
#smartzip的腳本,自動解壓bzip2.gzip,zip類型的壓縮文件
#其中$1表示傳遞給腳本的第一個參數值,例如smartzip article.zip,$1表示字符串article.zip.
#另外file可以辨別出一個給定文件的文件類型
#select 循環語句
pocket=()
select var in 跳跳糖 糖 很多糖 企鵝糖; do
echo "除了 $var 還要什麼嗎?"
if ((RANDOM%4==0)); then
echo "呀!時間不夠了,快上車!"
break
fi
pocket+=("$var")
done
echo "你最後說的那個$var弄丟了...."
IFS='、'
echo "現在口袋裏只有 ${pocket[*]}。"
IFS=$'\t\n'
some examples
#!/bin/bash
if [ $# -lt 3 ] ; then
cat << HELP
ren -- renames a number of files using sed regular expressions USAGE: ren 'regexp' 'replacement' files...
EXAMPLE: rename all *.HTM files in *.html:
ren 'HTM$' 'html' *.HTM
HELP
exit 0
fi
#第一個if表達式判斷輸入命令行參數是否小於3個 (特殊變量$# 表示包含參數的個數) 。
#如果存在則通過sed命令搜索和替換來產生新的文件名。
#這裏HELP要頂格寫,前面不能有空格或者TAB製表符。如果cat一行寫成cat << -HELP,前邊可以帶TAB.
OLD="$1"
NEW="$2"
shift
shift
#我們使用shift命令將第一個和第二個參數從參數列表中刪除,這樣原來的第三個參數就成爲參數列表$*的第一個參數。
for file in "$@"; do
if [ -f "$file" ] ; then
newfile='echo "$file" | sed "s/$(OLD)/$(NEW)/g"'
if [ -f "$newfile" ]; then
echo "error: $newfile exists alreadly"
else
echo "renaming $file to $newfile"
mv "$file" "newfile"
fi
fi
done
Rename example
#rename
for a in $( ls ./ );do
if [ $a == 'history.txt' ];then
# if [ $a != './test' ];then
mv $a $a.bak
fi
done
#test的文件名不修改
#給文件加上後綴 .bak
md2txt example
#!/bin/bash
find ./ -name "*.md" | while read i
do
echo "$i";
mv $i ${i%.*}.txt
done
txt2md example
#!/bin/bash
find ./ -name "*.txt" | while read i
do
echo "$i";
mv $i ${i%.*}.md
done