初级版本
vi adduser.sh
[root@linux1 sh]# cat addUser.sh
adduser $1
echo $1 | passwd --stdin $1
echo "输出成功!"
变为可执行文件
[root@linux1 sh]# chmod +x addUser.sh
执行文件
[root@linux1 sh]# ./addUser.sh niu05
Changing password for user niu05.
passwd: all authentication tokens updated successfully.
输出成功!
优化脚本
[root@linux1 sh]# vi addUser.sh
[root@linux1 sh]# cat addUser.sh
#! /bin/bash
[ ! $# -eq 1 ] && echo "args error !!!" && exit 1
id $1 >&/dev/null && echo "user has exits " && exit 2
adduser $1 && echo $1 | passwd --stdin $1 >&/dev/null && echo "user add succeful!" && exit 3
echo "user add fail !" && exit 4
[root@linux1 sh]# ./addUser.sh
args error !!!
[root@linux1 sh]# ./addUser.sh eee rrr nnn
args error !!!
[root@linux1 sh]# ./addUser.sh niu05
user has exits
[root@linux1 sh]# ./addUser.sh niu07
user add succeful!
>&/dev/null 输出到黑洞
echo $1 | passwd --stdin 静默输入
文件输出
[root@linux1 sh]# vi text.txt
[root@linux1 sh]# cat text.txt
111 aaa
222 bbb
333 ccc
[root@linux1 sh]# vi printText.sh
[root@linux1 sh]# cat printText.sh
#! /bin/bash
oldIFS=$IFS
IFS=$'\n'
num=0
for i in `cat text.txt`;do
echo $i;
((num++));
done
echo $num
IFS=$oldIFS
echo "--------------------------"
num=0
line=`cat text.txt | wc -l`
for((i=1;i<=$line;i++));do
str=`head -n $i text.txt | tail -1`
echo $str
((num++));
done
echo $num
echo "--------------------------"
num=0
while read line;do
echo $line
((num++));
done <text.txt
echo $num
[root@linux1 sh]# chmod +x printText.sh
[root@linux1 sh]# ./printText.sh
111 aaa
222 bbb
333 ccc
3
--------------------------
111 aaa
222 bbb
333 ccc
3
--------------------------
111 aaa
222 bbb
333 ccc
3
查找给定路径最大文件
[root@linux1 sh]# vi findMaxFile.sh
[root@linux1 sh]# cat findMaxFile.sh
#! /bin/bash
oldIFS=$IFS
IFS=$'\n'
for i in `du -ah $1 | sort -nr`;do
fileName=`echo $i | awk '{print $2}'`;
if [ -f $fileName ];then
echo $fileName;
break
fi
done
IFS=$oldIFS
[root@linux1 sh]# chmod +x findMaxFile.sh
[root@linux1 sh]# ./findMaxFile.sh
./.text.txt.swp
[root@linux1 sh]# ./findMaxFile.sh /tmp/
/tmp/.abc.swo