1、 删除/etc/grub.conf文件中行首的空白符
sed [email protected]^[[:space:]]*@@‘ /etc/grub.conf
2、 替换/etc/inittab文件中"id:3:initdefault:" 一行中的数字为5
3、 删除/etc/inittab文件中的空白行
sed /^$/d /etc/inittab
4、 删除/etc/inittab文件中开头的#号
sed ‘s/^#*//g‘ /etc/inittab
5、 删除某文件中开头的#号及后面的空白字符,但要求#号后面必须有空白字符
sed s/^#[[:space:]]*//g 123.txt
6、 删除某文件中以空白字符后面跟#类的行中的开头的空白字符及#
#abc
# hello world
#hi world
sed s/^[[:space:]]*#//g 123.txt
7、 取出一个文件路径的目录名称
/etc/fstab
/var/log
取出etc 和var
目录名:
echo "/etc/rc.d/" | sed -r ‘[email protected]^(/.*/)[^/]+/[email protected]\[email protected]‘
基名:
echo "/etc/rc.d/" | sed -r ‘[email protected]^/.*/([^/]+)/[email protected]\[email protected]‘
老师标准答案:
sed练习:
1、删除/etc/grub.conf文件中行首的空白符;
sed -r ‘[email protected]^[[:spapce:]][email protected]@g‘ /etc/grub.conf
2、替换/etc/inittab文件中"id:3:initdefault:"一行中的数字为5;
sed ‘[email protected]\(id:\)[0-9]\(:initdefault:\)@\15\[email protected]‘ /etc/inittab
3、删除/etc/inittab文件中的空白行;
sed ‘/^$/d‘ /etc/inittab
4、删除/etc/inittab文件中开头的#号;
sed ‘[email protected]^#@@g‘ /etc/inittab
5、删除某文件中开头的#号及后面的空白字符,但要求#号后面必须有空白字符;
sed -r ‘[email protected]^#[[:space:]][email protected]@g‘ /etc/inittab
6、删除某文件中以空白字符后面跟#类的行中的开头的空白字符及#
sed -r ‘[email protected]^[[:space:]]+#@@g‘ /etc/inittab
7、取出一个文件路径的目录名称;
echo "/etc/rc.d/" | sed -r ‘[email protected]^(/.*/)[^/]+/[email protected]\[email protected]‘
基名:
echo "/etc/rc.d/" | sed -r ‘[email protected]^/.*/([^/]+)/[email protected]\[email protected]‘
#abc
# hello world
# hi world
练习:
传递一个用户名参数给脚本,判断此用户的用户名跟其基本组的组名是否一致,并将结果显示出来。
#!/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
=================================================================
练习:写一个脚本
传递一个参数(单字符就行)给脚本,如参数为q、quit、Q、Quit,就退出脚本,否则, 就显示用户的参数:
#!/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
=======================================================================
练习:
传递三个参数给脚本,第一个为整数,第二个为算术运算符,第三个为整数,将计算结果显示出来,要求保留两位精度。形如:
./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
====================================================================
写一个脚本:
判断当前主机的CPU生产商,其信息在/proc/cpuinfo文件中vendor id一行中。
如果其生产商为AuthenticAMD,就显示其为AMD公司;
如果其生产商为GenuineIntel,就显示其为Intel公司;
否则,就说其为非主流公司;
#!/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
=====================================================================
写一个脚本:
给脚本传递三个整数,判断其中的最大数和最小数,并显示出来。
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"
======================================================================
练习:
传递3个参数给脚本,参数均为用户名。将此些用户的帐号信息提取出来后放置于/tmp/testusers.txt文件中,并要求每一行行首有行号。
echo "1 $LINES" >> / /tmp/testusers
echo "2 $LINES" >> / tmp/testusers
####脚本有问题###
!/bin/bash
#
for I in `seq 1 $#`;do
#echo "`cat /etc/passwd | grep $I`" >> /tmp/tempusers
echo $`$I`
done
=======================================================================
循环练习
向系统内所有用户问好:
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
======================================================================
写一个脚本:
1、设定变量FILE的值为/etc/passwd
2、依次向/etc/passwd中的每个用户问好,并显示对方的shell,形如:
Hello, root, your shell: /bin/bash
3、统计一共有多少个用户
扩展: 只向默认shell为bash的用户问好
#!/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
扩展: 只向默认shell为bash的用户问好,
扩展版:
#!/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"
=======================================================================
写一个脚本:
1、添加10个用户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
扩展:
接受一个参数:
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
====================================================================
写一个脚本:
计算100以内所有能被3整除的正整数的和;
取模,取余:%
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"
====================================================================
写一个脚本:
计算100以内所有奇数的和以及所有偶数的和;分别显示之;
#!/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"
=====================================================================
写一个脚本,分别显示当前系统上所有默认shell为bash的用户和默认shell为/sbin/nologin的用户,并统计各类shell下的用户总数。显示结果形如:
BASH,3users,they are:
root,redhat,gentoo
NOLOGIN, 2users, they are:
bin,ftp
可以追加到一个文件内,然后在 显示那个文件
#!/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`
=======================================================================