LABEL=MD0 /mydata ext4 defaults,noatime,acl 0 0 运行以下命令,根据/etc/fstab挂载设备:
# mount -a
5). 查看挂载状态:
7、写一个脚本
(1) 接受一个以上文件路径作为参数;
(2) 显示每个文件拥有的行数;
(3) 总结说明本次共为几个文件统计了其行数;
#!/bin/bash
#
if [ $# -eq 0 ];then
echo "Parameters cannot be empty!"
exit 1
fi
fileCount=0
for i in $@
do
if [ -f $i ]
then
linecount=($(wc -l $i))
echo "$i has $linecount lines."
let fileCount++
else
echo "$i not be regular file!"
fi
done
echo -e "Counted files: $fileCount \n" 结果:
8、写一个脚本
(1) 传递两个以上字符串当作用户名;
(2) 创建这些用户;且密码同用户名;
(3) 总结说明共创建了几个用户;
#/bin/bash
#
if [ ! $UID -eq 0 ]
then
echo "Please login root."
exit 1
fi
if [ $# -eq 0 ]
then
echo -e "Parameters cannot be empty!\n"
fi
userCount=0
for user in $@
do
if id $user &> /dev/null
then
echo "$user already exists."
else
useradd $user
[ $? -eq 0 ] && echo "$user" | passwd --stdin $user > /dev/null || echo -e "$user not created!\n"
echo "$user to create success!"
let userCount++
fi
done
echo -e "Total of created user: $userCount\n" 结果:
9、写一个脚本,新建20个用户,visitor1-visitor20;计算他们的ID之和;
#/bin/bash
#
declare -i userNum=1
declare -i sumUID=0
while [ $userNum -le 20 ] ; do
if id visitor$userNum &> /dev/null ; then
echo "visitor$userNum already existing!"
else
useradd visitor$userNum &> /dev/null && echo "visitor$userNum" | passwd --stdin visitor$userNum &> /dev/null
[ $? -eq 0 ] && echo "visitor$userNum created successfully." || echo "visitor$userNum Chris create failure!"
fi
echo "visitor$userNum UID is: $(id -u visitor$userNum)"
let sumUID+=$(id -u visitor$userNum)
let userNum++
done
echo "The sum of the UID: $sumUID" 结果:
10、写一脚本,分别统计/etc/rc.d/rc.sysinit、/etc/rc.d/init.d/functions和/etc/fstab文件中以#号开头的行数之和,以及总的空白行数;
#/bin/bash
#
declare -i sum1=0 # Sum for # lines
declare -i sum2=0 # Sum for null lines
for file in $@; do
if [ -f $file ]; then
s1=$(grep '^#' $file | wc -l)
s2=$(grep '^$' $file | wc -l)
echo "$file # lines: $s1"
echo "$file null lines: $s2"
sum1+=s1
sum2+=s2
else
echo "$file not found or not text file!"
fi
done
echo -e "\nTotal # lines: $sum1"
echo "Total null lines: $sum2" 结果:
11、写一个脚本,显示当前系统上所有默认shell为bash的用户的用户名、UID以及此类所有用户的UID之和;
#!/bin/bash
#
userList=$(grep '\/bin\/bash$' /etc/passwd)
declare -i sumUID=0
for user in $userList ; do
userName=$(echo $user | cut -d: -f1)
uid=$(echo $user | cut -d: -f3)
echo -e "User: $userName\tUID: $uid"
let sumUID+=$uid
done
echo "Sum UID: $sumUID" 结果:
12、写一个脚本,显示当前系统上所有,拥有附加组的用户的用户名;并说明共有多少个此类用户;
#!/bin/bash
#
allUsername=$(cut -d: -f1 /etc/passwd)
declare -i sumSG=0
echo 'There are supplementary groups of user: '
for user in $allUsername; do
if id $user | grep ',' &> /dev/null ; then
echo -n "$user "
let sumSG++
fi
done
echo -e "\nSum users: $sumSG" 结果: