20200321-shell學習指南

shell學習指南

查看可用的shell文件

cat /etc/shells  //查看可用的shell
echo $SHELL  //默認shell
 
echo "可用的shell"
cat /etc/shells

echo "默認shell"
echo $SHELL

some Notes about ‘for’ and ‘if’

#!/bin/bash

#for test

for var in A B C ; do
	echo "var is $var"
done

#for and if test
#打印一些rpm包的統計信息
for rpmpackage in "$@";do
	if [ -r "$rpmpackage" ]; then
		echo "========$rpmpackage=========="
		rpm -qi -p $rpmpackage
	else
		echo "ERROR: cannot read file $rpmpackage"
	fi
done

#特殊變量$@,該變量包含有輸入的所有命令行參數值。如果你運行showrpm openssh.rpm w3m.rpm webgrep.rpm,
#那麼 "$@"(有引號) 就包含有 3 個字符串,即openssh.rpm, w3m.rpm和 webgrep.rpm。
#$*的意思是差不多的。但是隻有一個字串。如果不加引號,帶空格的參數會被截斷。

# echo and regulation expression
echo *.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
# ...
#fi

if [ "${SHELL}"=="/bin/bash" ]; then
  echo "your login shell is the bash (bourne again shell)"
else
  echo "your login shell is not bash but ${SHELL}"
fi

if [ "${SHELL}" == "/bin/bash" ]; then
echo "your login shell is the bash (bourne again shell)"
else
echo "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 &lt;&lt; -HELP,前邊可以帶TAB.

OLD="$1"
NEW="$2"
shift
shift
#我們使用shift命令將第一個和第二個參數從參數列表中刪除,這樣原來的第三個參數就成爲參數列表$*的第一個參數。

for file in "$@"; do
	if [ -f "$file" ] &nbsp;; 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

update repo example

git add .
git commit -m 'backups'
git push origin master

update example

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