Shell編程之位置變量

       linux中命令可以接受參數,同樣的,shell腳本也可以接受參數。這些參數用$1、$2、$3…$n表示。

      $0  表示腳本本身

      $1  傳遞給腳本的第1個參數

$2  傳遞給腳本的第2個參數

$3  傳遞給腳本的第3個參數

      $n  傳遞給腳本的第n個參數

$#     表示傳遞的參數個數用

      $*     傳遞給腳本的全部參數,所有參數合為一個字符串

      $@   傳遞給腳本的全部參數,每個參數是一個獨立的字符串

               $@  $* 只在被雙引號包起來的時候才會有差異

示例:不同位置變量的引用方法。

blob.png

1、編寫腳本/root/bin/systeminfo.sh,顯示當前主機系統信息,包括主機名,IPv4地址,操作系統版本,內核版本,CPU型號,內存大小,硬盤大小。

#!/bin/bash
#author:zmingbo
#
system_release=`cat /etc/redhat-release`
ip_addr=`ifconfig  | sed -n -r 's@.*addr:(.*) Bcast.*$@\1@p'`
kernel_version=`uname -r`
cpuinfo=`cat /proc/cpuinfo | sed -r -n 's@model name.*:(.*)$@\1@p'`
memsize=`free -mh | grep Mem |tr -s " " | cut -d " " -f 2`
disksize=`fdisk -l| sed -n "/\/dev\/sd[a-z]:/p"|cut -d : -f 2 | 
            cut -d " " -f 2 | tr -s "\n" ":" | 
            sed  's@:\$@@'| sed  's@:@+@'` 
total_disksize=`echo $disksize |bc`

echo "The hostname is `hostname` "
echo "The release is $system_release "
echo "The kernel version is $kernel_version"
echo "The Disksize is $total_disksize G"
echo "The cpu is $cpuinfo"
echo "The mem size is $memsize"
for i in $ip_addr;do
	echo "IPv4 adress is $i "
done

blob.png

2、編寫腳本/root/bin/backup.sh,可實現每日將/etc/目錄備份到/root/etcYYYY-mm-dd中

#!/bin/bash
#
cp -a /etc/.  /root/etc`date +%Y-%m-%d`

blob.png

3、編寫腳本/root/bin/disk.sh,顯示當前硬盤分區中空間利用率最大的值

#!/bin/bash
use=`df -Th | sed -n '/\dev\/sd[a-z]\+[0-9]\+/p' | 
tr -s " " | cut -d " " -f 6 | sort -nr | head -n 1|tr -d "%"`
echo "The bigest utilization is $use"
if [ $use -gt 80 ];then
	wall "Disk will be full"
else
	echo "work well"
fi

blob.png

4、編寫腳本/root/bin/links.sh,顯示正連接本主機的每個遠程主機的IPv4地址和連接數,并按連接數從大到小排序

#!/bin/bash
#
netstat -nt | sed -n '/^[\(tcp\)]/p' |tr -s " " |cut -d " " -f 5 |
cut -d : -f 1 | sort -n | uniq -c | sort -nr

blob.png

5、寫一個腳本/root/bin/sumid.sh,計算/etc/passwd文件中的第10個用戶和第20用戶的ID之和

#!/bin/bash
#
user10id=`cat  /etc/passwd | head | tail -n1 | cut -d : -f 3`
user20id=`cat  /etc/passwd | head -n20 | tail -n1 | cut -d : -f 3`
idsum=$[$user10id+$user20id]
echo "The id sum is $idsum"

 blob.png

6、寫一個腳本/root/bin/sumspace.sh,傳遞兩個文件路徑作為參數給腳本,計算這兩個文件中所有空白行之和 

#!/bin/bash
#
if [ $# -lt 1 ];then
	echo "Two files needed"
	exit 222
fi

flines=`cat $1 |grep -E "^$"| wc -l`
slines=`cat $2 |grep -E "^$"| wc -l`

let sumlines=$flines+$slines
echo "Total empty lines  is $sumlines"

blob.png

7、寫一個腳本/root/bin/sumfile.sh,統計/etc, /var, /usr目錄中共有多少個一級子目錄和文件

#!/bin/bash
#
etc_dir_num=`ls -Al /etc|grep "^d.*" |wc -l`
var_dir_num=`ls -Al /var|grep "^d.*" |wc -l`
usr_dir_num=`ls -Al /usr|grep "^d.*" |wc -l`
total_dir_num=$[$etc_dir_num+$var_dir_num+$usr_dir_num]

etc_file_num=`ls -Al /etc | grep "^-.*" | wc -l`
var_file_num=`ls -Al /var | grep "^-.*" | wc -l`
usr_file_num=`ls -Al /usr | grep "^-.*" | wc -l`
total_file_num=$[$etc_file_num+$var_file_num+$usr_file_num]

etc_total_num=`ls -Al /etc |  wc -l`
var_total_num=`ls -Al /var |  wc -l`
usr_total_num=`ls -Al /usr |  wc -l`
total_num=$[$etc_total_num+$var_total_num+$usr_total_num]

echo "/etc & /var &/usr total  directory is $total_dir_num"
echo "/etc & /var &/usr total  file is $total_file_num"
echo "/etc & /var &/usr total  files and directory is $total_num"

blob.png 

8、寫一個腳本/root/bin/argsnum.sh,接受一個文件路徑作為參數;如果參數個數小于1,則提示用戶“至少應該給一個參數”,并立即退出;如果參數個數不小于1,則顯示第一個參數所指向的文件中的空白行數 

#!/bin/bash
#
if [ $# -lt 1 ];then
	echo " 1 asrg expected"
	exit 222
else
	sum=`cat $1 | grep "^$" |wc -l`
	echo "$1 has $sum empty lines "
fi

blob.png

9、寫一個腳本/root/bin/hostping.sh,接受一個主機的IPv4地址做為參數,測試是否可連通。如果能ping通,則提示用戶“該IP地址可訪問”;如果不可ping通,則提示用戶“該IP地址不可訪問” 

#!/bin/bash
#
if [ $# -lt 1 ];then
	echo -e "\033[31mOne ipaddr is expected\033[0m"
	exit 22
fi

if echo "$1" |grep -E -o "([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-4])\.
([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-4])\.
([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-4])\.
([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-4])" >/dev/null;
then
	ping -c1 -W1 $1 &> /dev/null && echo "$1 is reachable" || echo "$1 is unreachable"
else
	echo -e "\033[31mPlease enter correct ip adress\033[0m"
	exit 22
fi

blob.png

10、chmod -rw /tmp/file1,編寫腳本/root/bin/per.sh,判斷當前用戶對/tmp/fiile1文件是否不可讀且不可寫 

#!/bin/bash
#
file=/home/hadoop/test.txt
if [ -r $file ] && [ -w $file ];then
	echo "I can read & write $file"
else
	echo "I can't read or write $file"
fi

blob.png

11、編寫腳本/root/bin/nologin.sh和login.sh,實現禁止和充許普通用戶登錄系統。

1、nologin.sh
#!/bin/bash
#
file=/etc/nologin
if [ -e $file ];then
	echo "Common user can't login system already"
else
	touch $file
	echo "Common user can't login system now"
fi
2、login.sh
#!/bin/bash
#
file=/etc/nologin
if [ -e $file ];then
	rm -fr /$file > /dev/null
	echo "Common user could login system now!"
else
	echo "Common user could login system already"
fi

 

blob.png

12、寫一個腳本/root/bin/hostping.sh,接受一個主機的IPv4地址做為參數,先判斷是否合格IP,否,提示IP格式不合法并退出,是,測試是否可連通。如果能ping通,則提示用戶“該IP地址可訪問”;如果不可ping通,則提示用戶“該IP地址不可訪問”

#!/bin/bash
#
if [ $# -lt 1 ];then
	echo -e "\033[31mOne ipaddr is expected\033[0m"
	exit 22
fi

if echo "$1" |grep -E -o "([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-4])\.
([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-4])\.
([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-4])\.
([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-4])" >/dev/null;
then
	ping -c1 -W1 $1 &> /dev/null && echo "$1 is reachable" || echo "$1 is unreachable"
else
	echo -e "\033[31mPlease enter correct ip adress\033[0m"
	exit 22
fi

 

blob.png

13、計算1+2+3+…+100的值

#!/bin/bash
#
num=1
sum=0
while [ $num -le 100 ];do
	sum=$[$num+$sum]
	num=$[$num+1]
done
echo $sum

 

blob.png

14、計算從腳本第一參數A開始,到第二個參數B的所有數字的總和,判斷B是否大于A,否提示錯誤并退出,是則計算之

#!/bin/bash
#
sum=0
if [ $# -lt 2 ];then
	echo "Two int number needed"
	exit 222
elif [ $1 -eq 0 ] &> /dev/null || [ $1 -lt 0 ] &> /dev/null;then
	echo "Please enter two int number,and the second arg must bigger than the first arg"
	exit 22
elif [ $1 -gt 0 ] &> /dev/null && [ $2 -gt 0 ]  &>/dev/null && [ $2 -gt $1 ] &> /dev/null;then
	for  ((i=$1;i<=$2;i++));do
		sum=$[$sum+$i]
	done
	echo $sum
else
	echo "Please enter two int number,and the second arg must bigger than the first arg"
	exit 22
fi

blob.png

原創文章,作者:M20-1鐘明波,如若轉載,請注明出處:http://www.www58058.com/33575

(0)
M20-1鐘明波M20-1鐘明波
上一篇 2016-08-12
下一篇 2016-08-12

相關推薦

  • N27網絡班第6周作業

    1、復制/etc/rc.d/rc.sysinit文件至/tmp目錄,將/tmp/rc.sysinit文件中的以至少一個空白字符開頭的行的行首加#; :%s@^[[:space:]]\+@#&@g 2、復制/boot/grub/grub.conf 至/tmp目錄中,刪除/tmp/grub.conf文件中的行首的空白字符 :%s@^[[:space:]]…

    Linux干貨 2017-09-03
  • linux 加密和證書

    安全目標:機密性:明文傳輸的ftp, http,telnet 不安全數據完整性:身份驗證:可用性:安全技術:認證,授權,安全通信,審計密碼算法和協議:對稱加密,公鑰加密,單向加密,認證協議 1、對稱加密:加密,解密使用同一個秘鑰,效率高 DES:Data Encrption Standard, 56bit3DES:AES:AdvancedBlowfish缺點…

    2017-09-11
  • Linux進程及管理(1)

    Linux進程及管理(1) 內核的功用:進程管理、文件系統、網絡功能、內存管理、驅動程序、安全功能 Process: 運行中的程序的一個副本; 存在生命周期Linux內核存儲進程信息的固定格式:task struct 多個任務的的task struct組件的鏈表:task list 進程創建: init 父子關系 進程:都由其父進程創建 fork(), cl…

    Linux干貨 2015-05-28
  • rpm與yum

    rpm包及yum 包查詢 rpm -q –query  搭配別的選項可用來包查詢 -a -f -p rpmfile:針對尚未安裝的程序包文件做查詢操作   -p 選項參數為文件名(一定要指定路徑) 例子: rpm -qpl /media/Packages/zsh-4.3.11-4.el6.centos.2.x86_64.rpm…

    Linux干貨 2016-08-24
  • linux發行版介紹及其哲學思想

    Linux是一種自由和開源的unix-like操作系統。目前運用領域最廣泛、使用人數最多的操作系統。該操作系統的內核是Linus Torvalds在1991年10月5日首次發布。是一個多任務,多用戶的操作系統,廣泛應用在服務器,手機,平板電腦,電視,電子游戲等領域,我們生活中隨處都可以見到linux身影。 Linux誕生 1991 年,在芬蘭,赫爾辛基大學的…

    2017-09-16
  • Linux的發展史

    Linux的誕生 1987年荷蘭阿姆斯特丹Vrije大學的Andrew S.Tanenbaum 教授為了讓學生們更了解操作系統而參照Unix系統編寫了Minix系統。在1988年芬蘭赫爾辛基大學迎來了一位新生Linus Benedict Torvalds ,他在學習了Minix系統后,以此為平臺和指導開發出了Linux。在1991年8月Linus Toval…

    Linux干貨 2016-10-19

評論列表(2條)

  • 馬哥教育
    馬哥教育 2016-08-16 15:52

    作業完成的很好,總結這里需要用點心哦,慢慢來,可以先模仿別人的寫,嘗試著寫出優秀的博客。加油?。?!

    • M20-1鐘明波
      M20-1鐘明波 2016-08-16 20:19

      @馬哥教育好的,以后會加強知識點的總結。

欧美性久久久久