bash编程练习题及答案
1、 刪除/etc/grub.conf文件中行首的空白符
? ? ?sed s@^[[:space:]]*@@' /etc/grub.conf
?
2、 替換/etc/inittab文件中"id:3:initdefault:" 一行中的數(shù)字為5
3、 刪除/etc/inittab文件中的空白行
? ? ??sed /^$/d /etc/inittab
4、 刪除/etc/inittab文件中開(kāi)頭的#號(hào)
? ? ?sed 's/^#*//g' /etc/inittab
5、 刪除某文件中開(kāi)頭的#號(hào)及后面的空白字符,但要求#號(hào)后面必須有空白字符
? ? ?sed s/^#[[:space:]]*//g 123.txt
6、 刪除某文件中以空白字符后面跟#類的行中的開(kāi)頭的空白字符及#
? ? ? ? ?#abc
? ? ? ? ? # hello world
? ? ? ? ? ? ? ?#hi world
? ? ?sed s/^[[:space:]]*#//g 123.txt
7、 取出一個(gè)文件路徑的目錄名稱
? ? ?/etc/fstab
? ? ?/var/log
? ? ?取出etc 和var
目錄名: ?
echo "/etc/rc.d/" | sed -r 's@^(/.*/)[^/]+/?@\1@g'
基名:
echo "/etc/rc.d/" | sed -r?'s@^/.*/([^/]+)/?@\1@g'
老師標(biāo)準(zhǔn)答案:
sed練習(xí):
1、刪除/etc/grub.conf文件中行首的空白符;
sed -r 's@^[[:spapce:]]+@@g' /etc/grub.conf
2、替換/etc/inittab文件中"id:3:initdefault:"一行中的數(shù)字為5;
sed 's@\(id:\)[0-9]\(:initdefault:\)@\15\2@g' /etc/inittab
3、刪除/etc/inittab文件中的空白行;
sed '/^$/d' /etc/inittab
4、刪除/etc/inittab文件中開(kāi)頭的#號(hào);
sed 's@^#@@g' /etc/inittab
5、刪除某文件中開(kāi)頭的#號(hào)及后面的空白字符,但要求#號(hào)后面必須有空白字符;
sed -r 's@^#[[:space:]]+@@g' /etc/inittab
6、刪除某文件中以空白字符后面跟#類的行中的開(kāi)頭的空白字符及#
sed -r 's@^[[:space:]]+#@@g' /etc/inittab
7、取出一個(gè)文件路徑的目錄名稱;
echo "/etc/rc.d/" | sed -r 's@^(/.*/)[^/]+/?@\1@g'????
基名:
echo "/etc/rc.d/" | sed -r 's@^/.*/([^/]+)/?@\1@g'????
#abc
# hello world
?? # hi world
? ? ?
練習(xí):
傳遞一個(gè)用戶名參數(shù)給腳本,判斷此用戶的用戶名跟其基本組的組名是否一致,并將結(jié)果顯示出來(lái)。
#!/bin/bash
#
if ! id $1 &> /dev/null; then
? echo "No such user"
? exit 10
fi
if [ `id -n -u $1` == `id -n -g $1` ]; then
? echo "The same"
else
? echo "Not the same"
fi
=================================================================
練習(xí):寫一個(gè)腳本
傳遞一個(gè)參數(shù)(單字符就行)給腳本,如參數(shù)為q、quit、Q、Quit,就退出腳本,否則, 就顯示用戶的參數(shù):
#!/bin/bash
#
if [ $1 = 'q' ];then
? echo "Quitting"
? exit 0
elif [ $1 = 'Q'];then
? echo "Quiting"
? exit 1
elif [ $1 = 'Quit' ]; then
echo "Quitting"
? exit 2
elif [ $1 = 'QUIT' ]; then
? echo "quiting"
? exit 3
else
? echo "$1"
fi
=======================================================================
練習(xí):
傳遞三個(gè)參數(shù)給腳本,第一個(gè)為整數(shù),第二個(gè)為算術(shù)運(yùn)算符,第三個(gè)為整數(shù),將計(jì)算結(jié)果顯示出來(lái),要求保留兩位精度。形如:
./calc.sh 5 / 2
提示: ?
echo "scale=2;111/22;" | bc
bc <<< "scale=2;111/22"
#!/bin/bash
#
A=$1
x=$2
B=$3
result= echo "scale=2;$A$x$B;"|bc
echo $result
====================================================================
寫一個(gè)腳本:
判斷當(dāng)前主機(jī)的CPU生產(chǎn)商,其信息在/proc/cpuinfo文件中vendor id一行中。
如果其生產(chǎn)商為AuthenticAMD,就顯示其為AMD公司;
如果其生產(chǎn)商為GenuineIntel,就顯示其為Intel公司;
否則,就說(shuō)其為非主流公司;
#!/bin/bash
#
mycpu=`cat /proc/cpuinfo | grep vendor | cut -d: -f2 | head -1 | sed 's/^[[:space:]]//g'`
if [ $mycpu = "GenuineIntel" ]; then
? echo "INTEL chip inside"
elif [ $mycpu = "GenuineIntel" ]; then
? echo "AMD chip inside"
else
? echo "unkown cpu type"
fi
=====================================================================
寫一個(gè)腳本:
給腳本傳遞三個(gè)整數(shù),判斷其中的最大數(shù)和最小數(shù),并顯示出來(lái)。
MAX=0
MAX -eq $1
MAX=$1
MAX -lt $2
MAX=$2
用兩兩比較法
#!/bin/bash
#
MAX=0
MIN=0
if [ $1 -lt $2 ]; then
? MAX=$2
? MIN=$1
else
? MAX=$1
? MIN=$2
fi
if [ $MAX -lt $3 ]; then
? MAX=$3
else
? MIN=$3
fi
echo "Max number is $MAX, the min number is $MIN"
======================================================================
練習(xí):
傳遞3個(gè)參數(shù)給腳本,參數(shù)均為用戶名。將此些用戶的帳號(hào)信息提取出來(lái)后放置于/tmp/testusers.txt文件中,并要求每一行行首有行號(hào)。
echo "1 $LINES" >> / /tmp/testusers
echo "2 $LINES" >> / tmp/testusers
####腳本有問(wèn)題###
!/bin/bash
#
for I in `seq 1 $#`;do
? #echo "`cat /etc/passwd | grep $I`" >> /tmp/tempusers
? echo $`$I`
done
=======================================================================
循環(huán)練習(xí)
向系統(tǒng)內(nèi)所有用戶問(wèn)好:
LINES=`wc -l /etc/passwd | cut -d' ' -f1`
for I in `seq 1 $LINES`;do echo "Hello, `head -n $I /etc/passwd| tail -1 | cut -d: -f1`";done
======================================================================
寫一個(gè)腳本:
1、設(shè)定變量FILE的值為/etc/passwd
2、依次向/etc/passwd中的每個(gè)用戶問(wèn)好,并顯示對(duì)方的shell,形如:?
???? Hello, root, your shell: /bin/bash
3、統(tǒng)計(jì)一共有多少個(gè)用戶
擴(kuò)展: 只向默認(rèn)shell為bash的用戶問(wèn)好
#!/bin/bash
#
FILE=/etc/passwd
LINES=`wc -l /etc/passwd| cut -d' ' -f1`
for I in `seq 1 $LINES`;do
? echo "Hello,`cat /etc/passwd | head -n $I | tail -1 | cut -d: -f1 ` , Your shell is `cat /etc/passwd| head -n $I | tail -1 | cut -d: -f7`"
done
擴(kuò)展: 只向默認(rèn)shell為bash的用戶問(wèn)好,
擴(kuò)展版:
#!/bin/bash
#
SUM=0
FILE=/etc/passwd
LINES=`wc -l /etc/passwd| cut -d' ' -f1`
for I in `seq 1 $LINES`;do
? USER=`cat /etc/passwd | head -n $I | tail -1 | cut -d: -f1 `
? USHELL=`cat /etc/passwd| head -n $I | tail -1 | cut -d: -f7`
? if [[ $USHELL == '/bin/bash' ]]; then???? ? #if $USHELL is null, then you???? will get error with the???? sinlge[], so please use???? double[[]] when???? judge the string equation.
? echo "Hello, $USER , Your shell is $USHELL"
? SUM=$[$SUM+1]
? fi
done
echo "Total user is $SUM"
=======================================================================
寫一個(gè)腳本:
1、添加10個(gè)用戶user1到user10,密碼同用戶名;但要求只有用戶不存在的情況下才能添加;
#!/bin/bash
#
for I in `seq 1 10`; do
? if ! id user$I &> /dev/null; then
??? useradd user$I &> /dev/null
??? echo "user$I"| passwd --stdin user$I &> /dev/nul
? else
??? echo "user$I already existed, will not create"
? fi
done
擴(kuò)展:
接受一個(gè)參數(shù):
add: 添加用戶user1..user10
del: 刪除用戶user1..user10
其它:退出
#!/bin/bash
#
if [[ $1 == add ]]; then
? for I in `seq 1 10`; do
??? if ! id user$I &> /dev/null; then
????? useradd user$I &> /dev/null
????? echo "user$I"| passwd --stdin user$I &> /dev/nul
??? else
????? echo "user$I already existed, will not create"
??? fi
? done
elif [[ $1 == del ]];then
? echo "delete function is running"
? for I in `seq 1 10`;do
? userdel -rf user$I &> /dev/null
? done
fi
====================================================================
寫一個(gè)腳本:
計(jì)算100以內(nèi)所有能被3整除的正整數(shù)的和;
取模,取余:%
3%2=1
100%55=45
#!/bin/bash
#
SUM=0
for I in `seq 1 100`;do
? if [ $(($I%3)) = 0 ];then
??? SUM=$[$SUM+$I]
? fi
done
echo "The sum is $SUM"
====================================================================
寫一個(gè)腳本:
計(jì)算100以內(nèi)所有奇數(shù)的和以及所有偶數(shù)的和;分別顯示之;
#!/bin/bash
#
J=0
O=0
for I in `seq 1 100`;do
? if [ $(($I%2)) = 0 ]; then
??? J=$[$J+$I]
? else
??? O=$[$O+$I]
? fi
done
echo "SUM J is $J"
echo "SU O is $O"
=====================================================================
寫一個(gè)腳本,分別顯示當(dāng)前系統(tǒng)上所有默認(rèn)shell為bash的用戶和默認(rèn)shell為/sbin/nologin的用戶,并統(tǒng)計(jì)各類shell下的用戶總數(shù)。顯示結(jié)果形如:
BASH,3users,they are:
root,redhat,gentoo
NOLOGIN, 2users, they are:
bin,ftp
可以追加到一個(gè)文件內(nèi),然后在 顯示那個(gè)文件
#!/bin/bash
#
ushells=""
bash_count=0
nologin_count=0
LINES=`cat /etc/passwd | wc -l`
for I in `seq 1 $LINES`;do
? if [[ `cat /etc/passwd | head -n $I | tail -1 | cut -d: -f7` == /bin/bash ]]; then
?? echo " `cat /etc/passwd | head -n $I | tail -1 | cut -d: -f1`" >> /tmp/bash_users
?? bash_count=$[$bash_count+1]
? elif [[ `cat /etc/passwd | head -n $I | tail -1 | cut -d: -f7` == /sbin/nologin ]]; then?
??? echo " `cat /etc/passwd | head -n $I | tail -1 | cut -d: -f1`" >> /tmp/nologin_users
??? nologin=$[$nologin+1]
? fi
done
echo "BASH, $bash_countusers, they are:"
echo `cat /tmp/bash_users`
echo "NOLOGIN, $nologin_countusers,they are:"
echo `cat /tmp/nologin_users`
=======================================================================
轉(zhuǎn)載于:https://blog.51cto.com/richier/1632742
總結(jié)
以上是生活随笔為你收集整理的bash编程练习题及答案的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Plyr – 简单,灵活的 HTML5
- 下一篇: 2015 UESTC 数据结构专题H题