Linux命令列和shell指令碼編程寶典 Richard Blum

來源:互聯網
上載者:User

第一個指令檔

複製代碼 代碼如下:#!/bin/bash
echo "This is my first bash code!"
exit 0

重新導向符號和數學計算複製代碼 代碼如下:#!/bin/bash
echo -n "The time and date are: "
date
value1=100 #等號前後不允許出現空格
value2=$value1
echo -n "value1="
echo $value1
echo -n "value2="
echo $value2
ls -l | sort > out.txt #管道符號(|)和重新導向輸出符號>
ls -l >> out.txt #重新導向追加輸出符號>>
echo -n "wc<out.txt:"
wc < out.txt #重新導向輸入符號<
echo "sort<<EOF ... EOF"
sort << EOF #內建輸入重新導向<<
`date`
EOF
#數學計算
echo -n "expr進行計算:1+5="
expr 1+5
echo -n "使用方括弧進行計算:1+5="
echo $[1+5]
echo "使用bc計算機進行浮點運算"
var1=100
var2=200
var3=`echo "scale=4;$var1/$var2" | bc`
echo "$var1 / $var2 = $var3"
var4=71
var5=`bc<<EOF
scale=4
a1=($var1*$var2)
b1=($var3*$var4)
a1+b1
EOF`
echo "var5=$var5"
exit 0

使用test命令複製代碼 代碼如下:#!/bin/bash
#使用test命令
var1=10
var2=100
if [ $var1 -gt $var2 ]
then
echo "var1 grate var2"
else
echo "var2 grate var1"
fi
#只能比較整數
test_user=hanxi
if [ $USER = $test_user ]
then
echo "Welcome $test_user"
fi
str1=Hanxi
str2=hanxi
if [ $str1 \> $str2 ]
then
echo "$str1 > $str2"
else
echo "$str1 < $str2"
fi
if [ -n $str1 ]
then
echo "The string '$str1' is not empty"
else
echo "the string '$str1' is empty"
fi
#檢查檔案目錄
if [ -d $HOME ]
then
echo "your Home dir exists"
cd $HOME
ls -a
else
echo "there's a problem with your HOME dir"
fi
pwfile=/etc/shadow
if [ -f $pwfile ]
then
if [ -r $pwfile ]
then
tail $pwfile
else
echo "Sorry, I'm unable to reas the $pwfile file "
fi
else
echo "Sorry, the file $pwfile doesn't exist"
fi
if [[ $USER == h* ]]
then
echo "Hello $USER"
else
echo "Sorry, I don't know you"
fi

迴圈語句複製代碼 代碼如下:#!/bin/bash
for file in /home/hanxi/*
do
if [ -d "$file" ]
then
echo "$file is a directory"
elif [ -f "$file" ]
then
echo "$file is a file"
fi
done
var1=10
while [ $var1 -gt 0 ]
do
echo $var1
var1=$[ $var1 - 1 ]
done
var1=100
until [ $var1 -eq 0 ]
do
echo $var1
var1=$[ $var1 - 25 ]
done
#檔案資料的迴圈
IFSOLD=$IFS
IFS=$'\n'
for entry in `cat /etc/passwd`
do
echo "Values in $entry -"
IFS=:
for value in $entry
do
echo " $value"
done
done | more
for file in /home/hanxi/*
do
if [ -d "$file" ]
then
echo "$file is directory"
elif
echo "$file is a file"
fi
done > output.txt

讀取參數複製代碼 代碼如下:#!/bin/bash
name=`basename $0`
echo the commane entered is : $name
c_args=$#
echo count args:$c_args
#取最後一個參數
echo the last parameter is ${!#}
echo all parameter: $*
echo all parameter: $@
count=1
for param in "$@"
do
echo "\$@ parameter #$count = $param"
count=$[ $count + 1 ]
done
#getopts
while getopts :ab:c opt
do
case "$opt" in
a) echo "Found the -a option";;
b) echo "Found the -b option, with value $OPTARG";;
c) echo "Found the -c option";;
*) echo "Unknown option : $opt";;
esac
done
shift $[ $OPTIND - 1 ]
count=1
for param in "$@"
do
echo "Parameter $count: $param"
count=$[ $count + 1 ]
done
read -p "Please enter your age:" age
echo age:$age
if read -t 5 -p "Please enter your name: " name
then
echo "Hellp $name,welcome to my script"
else
echo
echo "sorry ,too slow!"
fi
read -n1 -p "Do you want to continue [Y/N]?" answer
case $answer in
Y | y) echo
echo " fine, continue on...";;
N | n) echo
echo OK,Good bye
exit;;
esac
echo "This is the end of the script"
read -s -p "Enter your password: " pass
echo
echo "Is your password really $pass?"
#讀取檔案
count=1
cat for.txt | while read line
do
echo "Line $count: $line"
count=$[ $count+1 ]
done
echo "Finished processing the file"

重新導向檔案描述符複製代碼 代碼如下:#!/bin/bash
#永久重新導向
exec 9>&2
exec 2>testerror
echo "this will in testerror">&2
exec 2<&9
exec 9<&0
exec 0<testin
count=1
while read line
do
echo "Line #$count:$line"
count=$[ $count + 1 ]
done
exec 0<&9
#重新導向檔案描述符
exec 3>&1
exec 1>testout
echo "this should store in the output file"
echo "along with this line."
exec 1>&3
echo "Now things should be back to nomarl"
exec 4<&0
exec 0<testin
count=1
while read line
do
echo "Line #$count:$line"
count=$[ $count + 1 ]
done
exec 0<&4
read -p "Are you done now?" answer
case $answer in
Y|y) echo "Goodbye";;
N|n) echo "continue...";
esac
#建立讀寫檔案描述符
exec 8<> testfile
read line <&8
echo "Read:$line"
echo "This is a test line" >&8
#關閉檔案描述符
exec 8>&-
#列出檔案描述服
#`/usr/sbin/lsof -a -p $$`|more
#禁止命令輸出
#2 > /dev/null
#建立本地臨時檔案
tempfile=`mktemp test.XXXXXX`
exec 4>$tempfile
echo "This is the first line">&3
exec 4>&-
#在/temp中建立臨時檔案
tmpfile=`mktemp -t tmp.XXXXXX`
echo "The temp file is located at:$tempfile"
cat $tempfile
rm -f $tempfile
#建立臨時檔案夾
tmpdir=`mktemp -d dir.XXXXXX`
cd $tmpdir
tempfile1=`mktemp temp.XXXXXX`
ls -l
cd ..
#記錄訊息
a=`date | tee testfile;\
cat testfile;\
date | tee -a testfile;\
cat testfile`

訊號處理複製代碼 代碼如下:#!/bin/bash
#訊號處理
trap "echo 'get a sign'" SIGINT SIGTERM
trap "echo byebye" EXIT
echo "This is a test program"
count=1
while [ $count -le 10 ]
do
echo "Loop #$count"
sleep 10
count=$[ $count+1 ]
done
echo "This is the end of the test program"
trap - EXIT#移除捕獲
#後台牧師運行
#./test6.sh &
#不使用終端的情況下運行指令碼
#nohup ./test6.sh &
#查看作業
#jobs
#重新啟動作業
#bg 2(作業序號)//後台
#fg 2//前台
#優先順序
#nice -n 10 ./test6.sh
#renice 10 -p 25904(進程號)
#預計時間運行at命令
#at -f test6.sh 20:00
#batch命令,系統平均負載低於0.8時運行,可以設定時間,比at命令更好
#corn表格可以設定迴圈運行,格式:
#min hour dayofmonth month dayofweek command
#每個月第一天運行:
#12 16 * * 1 command
#每個月最後一天運行:
#12 16 * * * if [ `date +%d =d tommorrow` = 01 ] ; then ; command

函數的使用複製代碼 代碼如下:#!/bin/bash
#函數
#使用傳回值
function func1
{
read -p "Enter a value: " value
echo $[ $value * 2 ]
}
result=`func1`
echo "the new value is $result"
#傳遞參數
function func2
{
echo $[ $1+$2 ]
}
result=`func2 2 2`
echo "the new result is $result"
#局部變數, 遞迴
function func3
{
if [ $1 -eq 1 ]
then
echo 1
else
local temp=$[ $1-1 ]
local result=`func3 $temp`
echo $[ $result*$1 ]
fi
}
read -p "Enter value:" value
result=`func3 $value`
echo "the factorial of $value is: $result"
#調用目前的目錄下到函數庫
#. ./myfuncs

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.