1.用cat进行拼接

读取、显示、拼接
1.1 拼接
##使用cat将输入文件的内容合标准输入拼接在一起
[root@server testsh]# echo 'Text through stdin' | cat - temp.txt 
Text through stdin
This is a sample text 1
This is a sample text 2

1.2 压缩空白行

##压缩和移除空白行
[root@server testsh]# cat multi_blanks.txt 
a

b 


c


d
[root@server testsh]# cat -s multi_blanks.txt 
a

b 

c

d
[root@server testsh]# cat -s multi_blanks.txt | tr -s '\n'
a
b 
c
d
##将制表符显示为^|
cat -T file.txt
##行号
[root@server testsh]# cat -n temp.txt 
     1  This is a sample text 1
     2  This is a sample text 2

2. 文件查找与文件列表

3.xargs命令

将从stdin接收到的数据重新格式化,再将其作为参数提供给其他命令
[root@server ~]# find / -name "*.sh" | xargs ls -l | cat > /home/testsh/out.txt
[root@server ~]# cat /home/testsh/out.txt 
-rwxr-xr-x. 1 root root    662 8月  19 2010 /etc/acpi/actions/power.sh
-rwxr-xr-x. 1 root root    935 11月 22 2013 /etc/bash_completion.d/gdbus-bash-completion.sh
-rwxr-xr-x. 1 root root    951 11月 22 2013 /etc/bash_completion.d/gsettings-bash-completion.sh
-rwxr-xr-x. 1 root root   3221 2月  22 2013 /etc/dhcp/dhclient.d/nis.sh

4.使用tr进行转换

##替换
[root@server ~]# echo "HELLO WHO IS THIS" | tr 'A-Z' 'a-z'
hello who is this
##加密与解密
[root@server ~]# echo 12345 | tr '0-9' '9876543210'
87654
[root@server ~]# echo 87654 | tr '9876543210' '0-9'
12345
##删除字符输出
[root@server ~]# echo "Hello 123 world 456" | tr -d '0-9'
Hello  world 
[root@server ~]# cat /home/testsh/temp.txt | tr -d '0-9'
This is a sample text 
This is a sample text

5.使用sort进行排序

6.分割文件与数据

7.把一个命令的结果作为变量的内容赋值

[root@server ~]# CMD=`ls`
[root@server ~]# echo $CMD
anaconda-ks.cfg install.log install.log.syslog out.txt stderr.txt stdout.txt
[root@server ~]# CMD1=$(pwd)
[root@server ~]# echo $CMD1
/root

##按日期打包文件夹
[root@server home]# tar -zcf $(date +%F).tar.gz /home/testsh
tar: 从成员名中删除开头的“/”
[root@server home]# ll
总用量 8
-rw-r--r--. 1 root root 1855 11月  2 09:04 2017-11-02.tar.gz
drwxr-xr-x. 3 root root 4096 11月  2 06:48 testsh
##变量输出的问题1,避免分歧使用{}
[root@server home]# a=1
[root@server home]# echo $a1

[root@server home]# echo ${a}_1
1_1
[root@server home]# echo 12_${a}
12_1

8.shell的特殊变量

8.1 特殊位置参数
$0:获取当前执行的shell脚本的文件名,如果执行脚本包含了路径,也就包括脚本路径
$n:获取当前执行的shell脚本的第N个参数,若n大于9,则用大括号括起来,$(10)
$#:获取当前执行的shell脚本的参数的总个数
$*:获取当前shell脚本的所有参数,
$@
8.2 特殊状态变量
$?:查询命令执行或软件安装是否成功,返回0为成功,非0为失败
$$:获取当前执行的shell脚本的进程号
$_:获取上一条命令的最后一个参数值
$!:获取上一次执行脚本的pid
    ##实现系统中多次执行某一脚本后的进程只有一个
    [root@server testsh]# cat pid.sh 
    #!/bin/bash
    pidpath=/tmp/a.pid
    if [ -f "$pidpath" ]
      then
        kill `cat $pidpath` >/dev/null 2>&1
        rm -f $pidpath
    fi
    echo $$ >$pidpath
    sleep 300

results matching ""

    No results matching ""