溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Shell腳本編程入門(mén)編寫(xiě)教程

發(fā)布時(shí)間:2021-09-30 14:32:34 來(lái)源:億速云 閱讀:115 作者:iii 欄目:開(kāi)發(fā)技術(shù)

這篇文章主要講解了“Shell腳本編程入門(mén)編寫(xiě)教程”,文中的講解內(nèi)容簡(jiǎn)單清晰,易于學(xué)習(xí)與理解,下面請(qǐng)大家跟著小編的思路慢慢深入,一起來(lái)研究和學(xué)習(xí)“Shell腳本編程入門(mén)編寫(xiě)教程”吧!

例子一:繪制特殊圖形

代碼如下:

#!/bin/bash
 
MAX_NO=0
 
echo -n "Enter Number between (5 to 9) : "
 read MAX_NO
 
if ! [ $MAX_NO -ge 5 -a $MAX_NO -le 9 ] ; then
 echo "WTF... I ask to enter number between 5 and 9, Try Again"
 exit 1
 fi
 
clear
 
for (( i=1; i=i; s-- ))
 do
 echo -n " "
 done
 for (( j=1; j=1; i-- ))
 do
 for (( s=i; s<=MAX_NO; s++ ))
 do
 echo -n " "
 done
 for (( j=1; j<=i; j++ ))
 do
 echo -n " ."
 done
 echo ""
 done
 
echo -e "\n\n\t\t\t Whenever you need help, Tecmint.com is always there"


你應(yīng)該不會(huì)被上述例子中的“關(guān)鍵字”困擾了,很多都是你熟悉的,或者從它們的名字可以猜出它們的意思,如“max”設(shè)定某個(gè)變量的最大值,“for”是一個(gè)循環(huán)。

輸出結(jié)果:

代碼如下:

[root@tecmint ~]# chmod 755 Special_Pattern.sh
[root@tecmint ~]# ./Special_Pattern.sh
Enter Number between (5 to 9) : 6
       .
      . .
     . . .
    . . . .
   . . . . .
  . . . . . .
  . . . . . .
   . . . . .
    . . . .
     . . .
      . .
       .
 
        Whenever you need help, Tecmint.com is always there


如果你有其它語(yǔ)言的編程基礎(chǔ),那么學(xué)習(xí)上面的腳本對(duì)你來(lái)說(shuō)應(yīng)該很容易。即使你是計(jì)算機(jī)方面的新手,這個(gè)學(xué)習(xí)過(guò)程也不會(huì)太難。

例子二:五顏六色的腳本

Linux終端也是支持五顏六色的,請(qǐng)看下面的腳本:

代碼如下:

#!/bin/bash
 
clear
echo -e "&#092;&#048;33[1m Hello World"
 # bold effect
echo -e "&#092;&#048;33[5m Blink"
       # blink effect
echo -e "&#092;&#048;33[0m Hello World"
 # back to noraml
 
echo -e "&#092;&#048;33[31m Hello World"
 # Red color
echo -e "&#092;&#048;33[32m Hello World"
 # Green color
echo -e "&#092;&#048;33[33m Hello World"
 # See remaing on screen
echo -e "&#092;&#048;33[34m Hello World"
echo -e "&#092;&#048;33[35m Hello World"
echo -e "&#092;&#048;33[36m Hello World"
 
echo -e -n "&#092;&#048;33[0m"
  # back to noraml
echo -e "&#092;&#048;33[41m Hello World"
echo -e "&#092;&#048;33[42m Hello World"
echo -e "&#092;&#048;33[43m Hello World"
echo -e "&#092;&#048;33[44m Hello World"
echo -e "&#092;&#048;33[45m Hello World"
echo -e "&#092;&#048;33[46m Hello World"
 
echo -e "&#092;&#048;33[0m Hello World"

輸出結(jié)果:

Shell腳本編程入門(mén)編寫(xiě)教程

你可以對(duì)上面的列子舉一反三,把它用到你自己的腳本中去。

例子三:加密文件/目錄

下面的例子演示了如何加密一個(gè)份文件或者文件夾。目前的這個(gè)版本的腳本有一些局限,例如你必須把它和你要加密的文件/目錄放到同一個(gè)文件夾下面。另外,你可能需要安裝“pinentry-gui”。在Fedora下安裝“pinentry-gui”的命令是:

代碼如下:

[root@midstage ~]# yum install pinentry-gui


在Ubuntu/Debian下安裝“pinentry-gui”的命令是:

代碼如下:

[root@midstage ~]# apt-get install pinentry-gui


創(chuàng)建一個(gè)腳本“Encrypt.sh”,將下面的代碼復(fù)制進(jìn)去。你也可以從這里下載這個(gè)腳本。

代碼如下:

#!/bin/bash
echo "Welcome, I am ready to encrypt a file/folder for you"
echo "currently I have a limitation, Place me to the same folder,
where a file to be encrypted is present"
echo "Enter the Exact File Name with extension"
read file;
gpg -c $file
echo "I have encrypted the file sucessfully..."
echo "Now I will be removing the original file"
rm -rf $file


輸出結(jié)果:

代碼如下:

[root@tecmint ~]# chmod 755 Encrypt.sh
[root@tecmint ~]# ./Encrypt.sh
 
Welcome, I am ready to encrypt a file/folder for you
currently I have a limitation, Place me to the same folder,
where a file to be encrypted is present
Enter the Exact File Name with extension
 
package.xml
 
                   Enter passphrase
 
                   Passphrase _________________________________
 
 
                   Please re-enter this passphrase
 
                   Passphrase _________________________________
 
 
I have encrypted the file successfully...
Now I will be removing the original file


代碼說(shuō)明:

gpg -c: 這個(gè)命令使用aka來(lái)加密文件。 在你需要的時(shí)候,你需要對(duì)加密的文件進(jìn)行解密。這里我們不給出具體的代碼了,你可以自己嘗試著寫(xiě)出來(lái)。提示:使用命令 gpg -d filename.gpg > filename 可以解密一份文件。

例子四:查看服務(wù)器利用率

查看服務(wù)器的利用率是管理員的一份重要的日常工作。聰明的管理員是知道如何是這份任務(wù)自動(dòng)化的。下面的這份腳本會(huì)抓取服務(wù)器的很多信息,快快試試吧!

代碼如下:

#!/bin/bash
date;
echo "uptime:"
uptime
echo "Currently connected:"
w
echo "--------------------"
echo "Last logins:"
last -a |head -3
echo "--------------------"
echo "Disk and memory usage:"
df -h | xargs | awk '{print "Free/total disk: " $11 " / " $9}'
free -m | xargs | awk '{print "Free/total memory: " $17 " / " $8 " MB"}'
echo "--------------------"
start_log=`head -1 /var/log/messages |cut -c 1-12`
oom=`grep -ci kill /var/log/messages`
echo -n "OOM errors since $start_log :" $oom
echo ""
echo "--------------------"
echo "Utilization and most expensive processes:"
top -b |head -3
echo
top -b |head -10 |tail -4
echo "--------------------"
echo "Open TCP ports:"
nmap -p- -T4 127.0.0.1
echo "--------------------"
echo "Current connections:"
ss -s
echo "--------------------"
echo "processes:"
ps auxf --width=200
echo "--------------------"
echo "vmstat:"
vmstat 1 5


輸出結(jié)果:

代碼如下:

[root@tecmint ~]# chmod 755 Server-Health.sh
[root@tecmint ~]# ./Server-Health.sh
 
Tue Jul 16 22:01:06 IST 2013
uptime:
22:01:06 up 174 days, 4:42, 1 user, load average: 0.36, 0.25, 0.18
Currently connected:
22:01:06 up 174 days, 4:42, 1 user, load average: 0.36, 0.25, 0.18
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
tecmint pts/0 116.72.134.162 21:48 0.00s 0.03s 0.03s sshd: tecmint [priv]
--------------------
Last logins:
tecmint pts/0 Tue Jul 16 21:48 still logged in 116.72.134.162
tecmint pts/0 Tue Jul 16 21:24 - 21:43 (00:19) 116.72.134.162
--------------------
Disk and memory usage:
Free/total disk: 292G / 457G
Free/total memory: 3510 / 3838 MB
--------------------
OOM errors since Jul 14 03:37 : 0
--------------------
Utilization and most expensive processes:
top - 22:01:07 up 174 days, 4:42, 1 user, load average: 0.36, 0.25, 0.18
Tasks: 149 total, 1 running, 148 sleeping, 0 stopped, 0 zombie
Cpu(s): 0.1%us, 0.0%sy, 0.0%ni, 99.3%id, 0.6%wa, 0.0%hi, 0.0%si, 0.0%st
 
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
1 root 20 0 3788 1128 932 S 0.0 0.0 0:32.94 init
2 root 20 0 0 0 0 S 0.0 0.0 0:00.00 kthreadd
3 root RT 0 0 0 0 S 0.0 0.0 0:14.07 migration/0

例子五:查看硬盤(pán)使用情況及發(fā)送提示郵件

下面的這個(gè)例子展示了當(dāng)硬盤(pán)的使用空間超出了預(yù)期設(shè)定的值時(shí),如果通過(guò)腳本來(lái)發(fā)送提示郵件。

代碼如下:

MAX=95
EMAIL=server@127.0.0.1
PART=sda1
 
USE=`df -h |grep $PART | awk '{ print $5 }' | cut -d'%' -f1`
if [ $USE -gt $MAX ]; then
echo "Percent used: $USE" | mail -s "Running out of disk space" $EMAIL
fi

說(shuō)明:將上述腳本中的“USER”替換成你的用戶名。你可以通過(guò)命令“mail”來(lái)查看你的郵件。

感謝各位的閱讀,以上就是“Shell腳本編程入門(mén)編寫(xiě)教程”的內(nèi)容了,經(jīng)過(guò)本文的學(xué)習(xí)后,相信大家對(duì)Shell腳本編程入門(mén)編寫(xiě)教程這一問(wèn)題有了更深刻的體會(huì),具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是億速云,小編將為大家推送更多相關(guān)知識(shí)點(diǎn)的文章,歡迎關(guān)注!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI