Linux 添加用户脚本 文件输出 查找给定路径最大文件

初级版本
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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值