1. 程式人生 > 其它 >簡單SHELL指令碼練習

簡單SHELL指令碼練習

技術標籤:Linux運維shell指令碼shell

1.查詢當前網段(10.1.1.0/24)記憶體活IP使用者,重定向到/tmp/ip.txt檔案中(ping)

#!/bin/bash
#查詢當前網段(10.1.1.0/24)記憶體活IP使用者,重定向到/tmp/ip.txt檔案中(ping)
for i in {1..254};do (		#將括號中的程式碼放入子shell執行
	ping -c1 -w1 10.1.1.$i > /dev/null 2>&1
	if [ $? -eq 0 ];then
		echo "10.0.1.$i is alive" >> /tmp/ip.txt
		echo
"10.0.1$i is alive" else echo "10.0.1.$i is dead" > /dev/null 2>&1 fi ) done

2.自動建立使用者student101-150,且密碼為password101-150

#!/bin/bash
#自動建立使用者student101-150,且密碼為password101-150
for i in {101..150};do
	id student$i > /dev/null 2>&1
	if [ $? -ne 0 ];then
		useradd student$i
		echo
"password$i" > passwd --stdin student$i fi done echo "使用者建立完成"

執行結果(部分截圖):

[root 15:48:[email protected] homework]$ (84) ./2.sh 
使用者建立完成
[root 15:48:[email protected] homework]$ (85) cat /etc/passwd | grep 'student'
student101:x:1009:1010::/home/student101:/bin/bash
student102:x:1010:1011::/home/student102:/bin/bash
student103:x:1011:1012::/home/student103:/bin/bash
student104:x:1012:1013::/home/student104:/bin/bash
student105:x:1013:1014::/home/student105:/bin/bash
student106:x:1014:1015::/home/student106:/bin/bash
student107:x:1015:1016::/home/student107:/bin/bash
student108:x:1016:1017::/home/student108:/bin/bash
student109:x:1017:1018::/home/student109:/bin/bash
student110:x:1018:1019::/home/student110:/bin/bash

3.根據passwd檔案內容,輸出“The line 1(行數) is root(使用者名稱)”依次類推

#!/bin/bash
#根據passwd檔案內容,輸出“The line 1(行數) is root(使用者名稱)”依次類推
n=1
while read line;do
	user=`echo $line | awk -F: '{print $1}'`
	echo "the line$n is $user"	
	let n++
done < /etc/passwd

執行結果:

[root 12:02:[email protected] homework]$ (38) ./4.sh 
the line1 is root
the line2 is bin
the line3 is daemon
the line4 is adm
the line5 is lp
the line6 is sync
the line7 is shutdown
the line8 is halt
the line9 is mail
the line10 is operator
...

4.寫一個指令碼,顯示當前系統上shell為-s指定型別的shell,並統計其使用者總數。-s選 項後面跟的引數必須是/etc/shells檔案中存在的shell型別,否則不執行此指令碼。另 外,此指令碼還可以接受–help選項,以顯示幫助資訊。執行及顯示結果如下: # ./test8.sh -s bash bash,3users,they are: root alice bob

#!/bin/bash
#顯示結果形如:
#./test7.sh -s bash
#bash,3users,they are:
#root redhat gentoo

case $1 in 
	-h|--help)
		echo "USAGE:./test7.sh [-h|--help] [-s shell_name]"  
	;;
	-s)
		shell=$2
		shell_flag=`grep "\b$shell\b" /etc/shells`
		if [ -n "$shell_flag" ];then
			user_number=`grep "\b$shell\b" /etc/passwd | wc -l`
			users=`grep "\b$shell\b" /etc/passwd | awk -F: '{print $1}'`
			echo  $shell,$user_number users,they are $users
		else
			echo "WARNING:no these shell in /etc/shells!"
			exit 1
		fi	
	;;
	*)
		echo "wrong arguments,use --help"
		exit 1
	;;
esac

執行結果:

[root 15:39:[email protected] homework]$ (78) ./7.sh -h 
USAGE:./test7.sh [-h|--help] [-s shell_name]
[root 15:39:[email protected] homework]$ (79) ./7.sh -s bash
bash,10 users,they are root yyk YYKK bash testbash basher user1 k1 k2 k3

5.ldd是用來檢視二進位制程式的共享庫檔案的,現在通過指令碼實現把庫檔案自動的複製 到固定目錄(/newroot)的相對應的目錄中,如源庫檔案:/lib/a.so.1,應複製 到/newroot/lib/a.so.1

#!/bin/bash

#ldd是用來檢視二進位制程式的共享庫檔案的,現在通過指令碼實現把庫檔案自動的複製到固定目錄(/newroot)的相對應的目錄中,如源庫檔案:/lib/a.so.1,應複製到/newroot/lib/a.so.1

read -p"輸入一個命令" commands
NEWROOT="/newroot"
#複製命令
cpbin(){
	BPATH=`which $commands | grep '/'`
	BDIR=${BPATH%/*}
	[ ! -d $NEWROOT$BDIR ] && mkdir -p $NEWROOT$BDIR
	[ -e $BPATH ] && cp -r $BPATH $NEWROOT$BDIR
}
#複製庫檔案
cplib(){
	ldd `which $commands | grep '/'` | grep -P -o '/lib(64){0,1}[^\s]{0,}' | while read line;do
		LPATH=$line
		LDIR=${line%/*}
		[ -d "$LDIR" ] && mkdir -p $NEWROOT$LDIR
		[ -e $LPATH ] && cp -r $LPATH $NEWROOT$LDIR
		echo "cp $LPATH $NEWROOT$LDIR finish"
	done	
}
cpbin
cplib

執行結果:

[root 15:02:[email protected] homework]$ (18) ./10.sh
輸入一個命令grep
cp /lib64/libpcre.so.1 /newroot
cp /lib64/libc.so.6 /newroot
cp /lib64/libpthread.so.0 /newroot
cp /lib64/ld-linux-x86-64.so.2 /newroot
[root 15:08:[email protected] /]$ (35) tree /newroot
/newroot
├── lib64
│	 ├── ld-linux-x86-64.so.2 -> ld-2.17.so
│	 ├── libc.so.6 -> libc-2.17.so
│	 ├── libpcre.so.1 -> libpcre.so.1.2.0
│	└── libpthread.so.0 -> libpthread-2.17.so
└── usr
    └── bin
        └── grep

3 directories, 5 files