您的位置:首页 > 运维架构 > Shell

菜鸟shell 脚本学习日记

2017-11-29 23:10 447 查看
1、shell 脚本统计当前目录下指定后缀文件行数:

获取当前目录下所有后缀为.txt 文件行数,包含空白行(包括子目录):

$find . -name "*.sh" |xargs cat |wc -l

获取当前目录下所有后缀为.txt 文件行数,除去空白行(包括子目录):

$find . -name "*.sh" |xargs cat |grep -v ^$ |wc -l

2、echo -e 转义字符串序列

eg:

echo  -e "1\t2\t3"

1 2
3

3、修改bash提示字符串(username@hostname:~$)

$ PS1="\u@\h:\w$"    #可以利用\e[1;31m 给提示字符串设置颜色,同样可以用"\e[1;41m" 设置提示字符串背景颜色。

eg:

PS1="\e[1;31m \u@\h:\w$ \e[1;42m"

4、将脚本内部的文本块进行重定向

eg:

#!/bin/bash

cat <<EOF> log.txt

LOG FILE HEADER

This is a test log file

Function: System statistics

EOF

$cat log.txt

LOG FILE HEADER

This is a test log file

Function: System statistics

5、自定义文件描述符(<读、>写、>> 追加)

创建一个文件描述符用于写入:

$exec 4 > output.txt

$echo  newline >&4

$cat output.txt
newline

6、shell脚本获取终端信息

tput 和stty 是两款终端处理工具,下面分别介绍:

1)获取终端行数和列数:

$tput cols

$tput lines

2)打印当前终端名:

$tput longname

3)将光标移动到坐标(100,100)处:

$tput cup 100 100

4)设置终端背景色:

$tput setb n

其中,n可以在0到7之间取值。

5)tput setf n

其中,n可以在0到7之间取值。

6)设置文本演示为粗体:

$tput bold

7)设置下划线的起止

$tput smul        //从此行开始具有下划线

$tput rmul        //下划线结束

8)删除从当前光标位置到行尾的所有内容:

$tput  ed

9)在输入密码时,不应该显示输入内容。下面使用stty来实现这一要求:

#!/bin/bash

#Filename : password.sh

echo -e "Enter password: "

stty -echo

read password

stty echo

echo

echo $password read.

/* 选项 -echo禁止将输出发送到终端,而选项echo则允许发送输出 */

7、脚本调试

1)使用选项-x,启用shell脚本的跟踪调试功能:

$bash -x test1.sh

2)使用set -x 和set +x对脚本进行部分调试,例如:

#!/bin/bash  

for i in {1..6};

do

        set -x

        echo=$i    

        set +x

done

echo "Script executed"

//只会打印出echo $i的调试信息,因为使用了-x和+x对调试进行了限制。

8、不使用回车键来读取N个字符(read 命令的高级应用)

1)从输入中获取n个字符存入val中:

$read -n -2  val      //读入2个字符存入val,结束此命令

$echo $val

2)无回显方式读取密码:

$read -s val

3)输入时打印显示提示信息:

$read -p "Enter input:"  val

4)在特定时限内读取输入,超时命令退出,相应值为空:

$read -t 2 val    //2s内输入,存入val

5)用特定的限定符作为输入行的结束符:

$read -d ":" val 

hello: //val 被设置为hello

9、小技巧,运行命令直至执行成功

eg:

1)方法一:

repeat()

{

while ture

do

$@ && return

done

}

#运行,如:

repeat  ls     #执行ls命令,并结束

repeat  aaa  #提示aaa: command not found ,并一直循环执行。

2)方法二:

#此方法使用shell内建的":",命令,尽管可读性不高,但是肯定比第一种方法块。

repeat()

{

while :

do

$@ && return

done

}

10、find 命令重要使用场景

1) 利用find执行命令或动作

find 和借助选项-exec和其他命令组合。

eg:寻找root用户全部文件,并将所有权修改为newman

#find  . -type f -user root -exec chown newman {} \;      // {} 是一个与-exec选项搭配使用的特殊字符串,会被替换为find 最终搜索的文件名。

eg:将目录所有c程序文件拼接起来写入单个文件all_c_files.txt

#find . -type f -name "*.c"  -exec cat {} \; > all_c_files.txt

eg: 让find跳过指定目录

#find .  \( -name  ".git" -prune \)  -o \( -type f -print \)

11、玩转xargs ,xargs 应该紧跟在管道操作符之后,以标准输入作为主要的源数据流。

1)将多行输入转换为单行输出

[root@localhost workplace]# cat char.sh 

1 2 3 4 5 6 

7 8 9 

10 11 12

[root@localhost workplace]# cat char.sh  | xargs

1 2 3 4 5 6 7 8 9 10 11 12

 2)将单行输入转换为多行输出

[root@localhost workplace]# cat char.sh  | xargs -n 3

1 2 3

4 5 6

7 8 9

10 11 12

3)可以用自己的界定符来分隔参数。用 -d 选项为输入指定一个定制的定界符。

[root@localhost workplace]# echo "splitXsplitXsplitXsplit" | xargs -d X

split split split split

//多选项结合:

[root@localhost workplace]# echo "splitXsplitXsplitXsplit" | xargs -d X -n 2

split split

split split

//统计源代码目录中所有C程序的文件按行数

#find  src/ -type f -name "*.c" -print0 | xargs -0 wc -l

12、加密工具与散列,linux常用的加解密工具如:crypt、gpg、base64、sha1sum

1)gpg 是一种应用广泛的工具,它使用加密技术来保护文件,以确保数据在送达目的地之前无法被读取。

eg:

$gpg -c test.txt  #加密,该命令采用交互式读取口令,在当前目录下生成test.txt.gpg

$gpg test.txt.gpg  #输入加密时的口令对文件进行解密,生成test.txt即源文件。

13、排序、唯一与重复

1)一组文件进行排序

$sort file1 file2  > sorted.txt  或者 $sort file1 file2  -o  sorted.txt

2)按照数字顺序进行排序

$sort -n file.txt

3)按照逆序进行排序

$sort -r file.txt

4)按照月份进行排序

$sort -M months.txt

5)合并两个已排序过的文件

$sort -m sorted1.txt sorted2.txt

6)找出已排序文件中不重复的行

$sort file1.txt file2.txt | uniq

7)检查文件是否已经排序过

#!/bin/bash

#功能描述:排序

sort -C filename;

if [ $? != 0 ]; then

    echo Sorted;

else

    echo Unsorted;

fi

8)依据键或列进行排序

#依据第一列按照数字,以逆序进行排序

$sort -nrk 1 data.txt

#依据第二列进行排序

$sort -k  2 data.txt

# -b忽略文件中的前导空白行,-d 用于指明以字典序进行排序。

$sort -bd unsorted.txt

9)、uniq ,此命令找出唯一行,用于排过序的数据输入。

$sort  unsorted.txt | uniq  #只显示唯一的行

$sort  unsorted.txt | uniq -c  #统计各行在文件中出现的次数

$sort  unsorted.txt | uniq -d  #找出文件中重复的行

采用 -s 和 -w来指定键:

# -s 指定可以跳过前n个字符

#-w 指定用于比较的最大字符数

eg:

$sort unsorted.txt | uniq -s 2 -w 2
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  shell 脚本