PJzhang:鳥哥的linux私房菜-shell指令碼-上

PJzhang發表於2020-10-11

 貓寧~~~

 

建議全程在centos7中進行,我在kali linux中有些命令無法執行。

 

1~家目錄下建立bin檔案,test.sh檔案在bin目錄

下面的shell程式碼列印Hello World!

#!/bin/bash
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
echo -e "Hello China! \a \n"
exit 0

2~輸入名稱,變數前空格一定要嚴格空出來哦

#!/bin/bash
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
read -p "Please input your first name:" firstname
read -p "Please input your last name:" lastname
echo -e "\nYour full name is:${firstname} ${lastname}"

sh test.sh

echo ${firstname} ${lastname},空

source test.sh

echo ${firstname} ${lastname},不空

3~批量建立檔案,自定義名稱之後新增時間,設定大量變數

不定義檔名字首,預設是filename,直接回車即可

#!/bin/bash
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
echo -e "I will use 'touch' command to create 3 files."
read -p "Please input your filename:" fileuser
filename=${fileuser:-"filename"}
date1=$(date --date="2 days ago" +%Y%m%d)
date2=$(date --date="1 days ago" +%Y%m%d)
date3=$(date +%Y%m%d)
file1=${filename}${date1}
file2=${filename}${date2}
file3=${filename}${date3}
touch "${file1}"
touch "${file2}"
touch "${file3}"

4~兩個數相乘運算

#!/bin/bash
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
echo -e "You should input 2 numbers,I will multiplying them! \n"
read -p "first number:" firstnu
read -p "second number:" secnu
total=$((${firstnu}*${secnu}))
echo "\nThe result of ${firstnu}*${secnu} is ==> ${total}"

 

echo $((120*120))   乘積,整數計算

echo $((13%3))   取餘

echo "1.2*1.2" | bc   小數計算,新增bc,bc在centos7上可以用,但是在kali linux上不可以

5~計算π的值,4*a(1)是bc提供的計算π的函式

#!/bin/bash
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
echo -e "This program will calculate pi value. \n"
echo -e "You should input a float number to calculate pi value. \n"
read -p "The scale number (10~10000):" checking
num=${checking:-"10"}
echo "Starting calcuate pi value. Be patient."
time echo "scale=${num}; 4*a(1)" | bc -lq

6~test判斷

目錄是否存在

在/root下建立test目錄

test -e /test && echo "exist" || echo "Not exist"

列印exist

test -e /root/test && echo "exist" || echo "Not exist"

列印Not exist

-e   檔案或目錄

-f   針對檔案存在與否

-d   針對目錄存在與否

檔案許可權相關

test -s /root/test.html && echo "exist" || echo "Not exist"

-r   可讀

-w   可寫

-x   可執行

-s   檔案是否為空

檔案比較

test1.html更新,列印exist

test test1.html -nt test.html && echo "exist" || echo "Not exist"

-nt    更新

-ot   更舊

-ef   是否為同一個檔案

整數間判定

test 1 -eq 1 && echo "exist" || echo "Not exist"   列印exist

-eq  相等

-ne   不等

-gt   前者大於後者

-lt   前者小於後者

-ge   前者大於等於後者

-le   前者小於等於後者

字串判定

test -z 111 && echo "exist" || echo "Not exist"   列印Not exist

-z   字串為空顯示exist,其他顯示Not exist

-n   字串為空顯示Not exist,其他顯示exist

test 111 == 111 && echo "exist" || echo "Not exist"

111 == 111   相等,列印exist,不相等,列印Not exist

111 != 111   相等,列印Not exist,不相等,列印exsit

多重條件判定

-rw-r--r--. 1 root root 5556 Oct 11 20:49 test.html

test -r test.html -a -x test.html && echo "exist" || echo "Not exist"   列印Not exist

test ! -x test.html && echo "exist" || echo "Not exist"   沒有執行許可權,列印exist

-a   and關係

-o   or關係

!   相反

#!/bin/bash
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
echo -e "Please input a filename,I wiil check the filename's type and permission. \n\n"
read -p "Input a filename:" filename
test -z ${filename} && echo "You must input a filename." && exit 0
test ! -e ${filename} && echo "The filename '${filename}' DO NOT exist" && exit 0
test -f ${filename} && filetype="regulare file"
test -d ${filename} && filetype="directory"
test -r ${filename} && perm="readable"
test -w ${filename} && perm="${perm} writable"
test -x ${filename} && perm="${perm} executable"
echo "The filename:${filename} is a ${filetype}"
echo "And the permissions for you are:${perm}"

檔名為空顯示

You must input a filename.

目錄

Input a filename:test
The filename:test is a directory
And the permissions for you are:readable writable executable

檔案不存在

Input a filename:120
The filename '120' DO NOT exist

檔案存在

Input a filename:test.html
The filename:test.html is a regulare file
And the permissions for you are:readable writable

7~判斷符號

[ "abcd" == "abcd" ] ; echo $?   列印0

[ "abcd" == "abc" ] ; echo $?   列印1

#!/bin/bash
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
read -p "Please input (Y/N):" yn
[ "${yn}" == "Y" -o "${yn}" == "y" ] && echo "OK,continue" && exit 0
[ "${yn}" == "N" -o "${yn}" == "n" ] && echo "Oh,interrupt" && exit 0
echo "I don't know what your choice is" && exit 0

 8~預設變數

[root@pjzhang ~]# file /etc/init.d/network
/etc/init.d/network: Bourne-Again shell script, ASCII text executable

檔案是可執行指令碼

/etc/init.d/network   stop,start,status

$0   指令碼檔名

$#   後接引數個數

$@   引數全部內容,加雙引號

${1}   第一個引數

#!/bin/bash
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
echo "The script name is    ==>${0}"
echo "Total parameter number is    ==>$#"
[ "$#" -lt 2 ] && echo "The number of parameter is less than 2. Stop here." && exit 0
echo "Your whole parameter is    ==>'$@'"
echo "The 1st parameter    ==>${1}"
echo "The 2st parameter    ==>${2}"

 第一次偏移掉1個,第二次在第一次基礎上偏移掉3個,從前向後

#!/bin/bash
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
echo "Total parameter number is     ==>$#"
echo "Your whole parameter is    ==>'$@'"
shift
echo "Total parameter number is     ==>$#"
echo "Your whole parameter is    ==>'$@'"
shift 3
echo "Total parameter number is     ==>$#"
echo "Your whole parameter is    ==>'$@'"

 

 

over~~~

 

相關文章