shell脚本练习

  1. 打印nowcoder.txt文件的最后5行
tail -5 1.txt
  1. 输出7的倍数
for i in {0..500..7}
do
    echo $i
    done
  1. 统计nowcoder.txt文件的行数
cnt=0;
while read val
do 
    cnt=$((cnt+1))
done < nowcoder.txt
echo $cnt
  1. 输出nowcoder.txt第5行的内容
line=0
while read var;
do
    line=$((line+1))
    if [ $line -eq 5 ];
    then 
        echo $var
    fi
done < nowcoder.txt
  1. 打印空行的行号
line=1;
while read val;
do
    if [ -z $val ];
    then     
            echo $line;
    fi
    line=$((line+1));
    #line=$[line+1];
done < nowcoder.txt
  1. 去掉空行
while read line
do
    if [ ! -z $line ];
    then 
        echo $line;
    fi
done < nowcoder.txt
  1. 打印字母数小于8的单词
#!/bin/sh
read line < nowcoder.txt
for i in $line
do
  if [ ${#i} -lt 8 ]
  then
    echo $i
  fi
done
  1. 统计所有进程占用内存和的大小
ps aux > nowcoder.txt
awk '{sum=sum+$6} END{print sum}' nowcoder.txt
  1. 求平均值
    test.txt
4
1
2
9
8
awk '{
     if(NR==1) total=$1;
     else sum+=$1
}
    END{
        printf("%.3f",sum/total);
    }' $1 test.txt
  1. 查看内存
ps -ef 
ps aux
top 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值