Draymonders / Code-Life

The marathon continues though.
27 stars 3 forks source link

bash 练习 #43

Open Draymonders opened 4 years ago

Draymonders commented 4 years ago

刷一些题,巩固基础 学到的一些命令

Draymonders commented 4 years ago

Tutorial

找到本地的一个代码目录,统计里面.cpp文件(或者.java .py等等)的个数

ls <file> | grep ".cpp" | wc -l

用下面的命令在/tmp目录生成一个脚本文件1.sh

printf '#!/bin/bash\ngrep "processor\|model name" /proc/cpuinfo\n' > /tmp/1.sh

processor   : 0
model name  : Intel(R) Core(TM) i5-6300HQ CPU @ 2.30GHz
processor   : 1
model name  : Intel(R) Core(TM) i5-6300HQ CPU @ 2.30GHz
processor   : 2
model name  : Intel(R) Core(TM) i5-6300HQ CPU @ 2.30GHz
processor   : 3
model name  : Intel(R) Core(TM) i5-6300HQ CPU @ 2.30GHz

把文件foo_bakup.txt改名为foo_bakup_YYYYMMDD.txt 其中YYYYMMDD是当天日期:年/月/日

mv foo_bakup.txt foo_bakup_`date +%Y%m%d`.txt

用下面的命令在/tmp目录中生成文件1.txt和2.txt

seq 1 100 > /tmp/1.txt; seq 2 101 > /tmp/2.txt 查看1.txt和2.txt的不同

ll / | awk 'NR == 1 {next} { print $6" "$7 }' | sort | uniq -c 这里NR 指代的是行号


      2 Apr 20
      1 Apr 21
      1 Apr 26
      1 Apr 29
      5 Aug 6
      1 Feb 2
      4 Feb 3
      5 Jun 7
      8 Mar 10
      1 May 18
      1 May 6

用uname -a可以看到操作系统的一些信息,用空格分割

uname -a | sed 's# #\n#g' sed命令 详见 https://github.com/Draymonders/Code-Life/issues/25

保存find结果

写一个脚本,命令行参数是1-2个文件名

参数个数

num=$#

if [ $num == 0 ]; then echo "请输入1-2个参数,代表文件" elif [ $num == 1 ]; then if [ -f $1 ]; then cat $1 else echo "文件不存在" fi elif [ $num == 2 ]; then if [ -f $1 ] && [ -f $2 ]; then diff -u $1 $2 else echo "文件不存在" fi fi