2021-05-12 14:32:11
Linux基本命令練習:指令碼實現硬碟分割區功能
練習一
1、通過ping命令測試10.109.134.233到10.109.134.249之間的所有主機是否線上;
如果線上,就顯示“ip is up ” 其中的IP要換成真正的IP地址,且以綠色顯示;
如果不線上,就顯示“ip is down”其中的IP要換成真正的IP地址,且以紅色顯示;
要求:分別使用whil,until和for(兩種形式)迴圈實現。
for第一種形式
#!/bin/bash
# program:
# 測試ip地址是否線上
# history donggen 2016-11-08-15:55
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bash
export PATH
for ((i=233;i<249;i++));do
ping -c 1 -W 1 10.109.134.$i
P=$?
[ $P -eq 0 ] && echo " 33[32m10.109.134.$i 33[0m is up" || echo -e " 33[31m 10.109.134.$i 33[0m is down"
done
for第二種形式
#!/bin/bash
# program:
# 測試ip地址是否線上
# history donggen 2016-11-08-15:55
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bash
export PATH
for I in {233..249}; do
ping -c 1 -W 1 10.109.134.$I &> /dev/null
RESULT=$?
[ $RESULT -eq 0 ] && echo -e" 33[32m 10.109.134.$I is up 33[0m" || echo -e" 33[31m 10.109.134.$I is down 33[0m"
done
until迴圈
#!/bin/bash
# program:
# 練習使用until迴圈進行ping命令
#history donggen 2016-11-08-17:30
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bash
export PATH
declare -i i=232
until [ $i -ge 249 ] ; do
let i+=1
ping -c 1 -W 1 10.109.134.$i &> /dev/null
RESULT=$?
[ $RESULT -eq 0 ] && echo -e " 33[32m10.109.134.$i is up. 33[0m" || echo -e " 33[31m10.109.134.$i is down. 33[0m"
done
while迴圈
#!/bin/bash
#program:
#練習使用while迴圈
#history donggen 2016-11-09-08:45
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bash
export PATH
declare -i i=232
while [ $i -ge 232 -a $i -le 249 ]; do
let i++
ping -c 1 -W 1 10.109.134.$i &> /dev/null
RESULT=$?
[ $RESULT -eq 0 ] && echo -e " 33[32m10.109.134.$i is up. 33[0m" || echo -e " 33[31m10.109.134.$i is down. 33[0m"
done
練習二
2、寫一個指令碼(前提:請為虛擬機器新增一塊硬碟,假設它為/dv/sdb),為指定的硬碟建立分割區;
1、列出當前系統上所有的磁碟,讓使用者選擇,如果選擇quit則退出指令碼;如果使用者選擇錯誤,就讓使用者重新選擇;
2、當使用者選擇後,提醒使用者確認接下來的操作可能會損壞資料,並請使用者確認;如果使用者選擇y就繼續,否則讓使用者重新選擇;
3、抹除那塊硬碟上的所有分割區(提示,抹除所有分割區後執行sync命令,並讓指令碼睡眠3秒
鐘後再分割區);並為其建立三個主分割區,第一個為20M,第二個為512M,第三個未128M,且第三個為swap分割區型別;(提示:將分割區命令通過echo傳送給fdisk即可實現)
#!/bin/bash
# program:
# 練習指令碼分割區
# history donggen 2016-11-09-9:10
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bash
export PATH
fdisk -l 2> /dev/null | grep "^Disk /dev/[sh]d[a-z]" | awk -F: '{print $1}'
read -p "Input your choice: " CHOICE
if [ $CHOICE = 'quit' ]; then
echo "choice is quiting.."
exit 6
fi
until fdisk -l 2> /dev/null | grep -o "Disk /dev/[sh]d[a-z]" | grep "^Disk $CHOICE" &> /dev/null; do
read -p "Input your choice again: " CHOICE
done
read -p "Please Continue y|n:" RESULT
until [ $RESULT == "y" -o $RESULT == "n" ]; do
read -p "Please Continue again y|n:" RESULT
done
if [ $RESULT == "n" ]; then
echo "Quiting..."
exit 7
elif [ $RESULT == "y" ]; then
dd if=/dev/zero of=$CHOICE bs=512 count=1
sync
sleep 3
echo 'n
p
1
+20M
n
p
2
+512M
n
p
3
+128M
t
3
82
w'| fdisk $CHOICE &> /dev/null
partprobe $CHOICE
sync
sleep 3
mke2fs ${CHOICE}1 &> /dev/null
mke2fs ${CHOICE}2 &> /dev/null
mkswap ${CHOICE}3 &> /dev/null
echo "$CHOICE is fdisk ok"
fi
本文永久更新連結地址:http://www.linuxidc.com/Linux/2016-11/136964.htm
相關文章