shell编程

Linux 中有好多中不同的shell,但是通常我们使用bash (bourne again shell) 进行shell编程,因为bash是免费的并且很容易使用。

脚本通常以下语句开始:

#!/bin/sh

符号#!用来告诉系统它后面的参数是用来执行该文件的程序

cd  / 是回到根目录

cd  当前用户目录

 sh 脚本文件:即使脚本文件没有执行权限,也可以执行

[root@host myshell]# /root/myshell/hello.sh
hello
[root@host myshell]# chmod -x hello.sh     
[root@host myshell]# /root/myshell/hello.sh
-bash: /root/myshell/hello.sh: Permission denied

[root@host myshell]# sh hello.sh
hello
[root@host myshell]# chmod +x hello.sh    
[root@host myshell]# /root/myshell/hello.sh
hello

在进行shell编程时,以#开头的句子表示注释,直到这一行的结束

shell不需声明变量直接使用即可,如下:

[root@host myshell]# cat hello.sh
name="xiaoma"
echo "hello,my name is  $name"
[root@host myshell]# sh hello.sh 
hello,my name is  xiaoma

有时候变量名很容易与其他字符混淆,可以使用花括号来告诉shell哪个是变量

[root@host myshell]# cat hello.sh
name="xiaoma"
echo "hello,my name is  ${name}"
[root@host myshell]# sh hello.sh 
hello,my name is  xiaoma

[root@host myshell]# cat hello.sh
name="xiaoma"
age=100
echo "hello,my name is  ${name},I'm ${age} years old."
[root@host myshell]# sh hello.sh 
hello,my name is  xiaoma,I'm 100 years old.

 /bin 存放可以执行的系统指令

[root@host ~]# cd /bin
[root@host bin]# ls
arch      chown  df             env         gawk      iptables-xml        logger  more           ping      rm       sh     taskset          uname          ypdomainname
awk       cp     dmesg          ex          grep      iptables-xml-1.4.7  login   mount          ping6     rmdir    sleep  touch            unicode_start  zcat
basename  cpio   dnsdomainname  false       gtar      kbd_mode            ls      mountpoint     plymouth  rpm      sort   tracepath        unicode_stop
bash      cut    domainname     fgrep       gunzip    kill                lsblk   mv             ps        rvi      stty   tracepath6       unlink
cat       dash   dumpkeys       find        gzip      link                mkdir   netstat        pwd       rview    su     true             usleep
chgrp     date   echo           findmnt     hostname  ln                  mknod   nice           raw       sed      sync   ulockmgr_server  vi
chmod     dd     egrep          fusermount  ipcalc    loadkeys            mktemp  nisdomainname  readlink  setfont  tar    umount           view

常用命令:

wc (选项)(参数) 

选项:

-c或--bytes或——chars:只显示Bytes数;
-l或——lines:只显示列数;
-w或——words:只显示字数。

参数:需要统计的文件列表,可以是一个或者多个文件

[root@host myshell]# wc -c hello.sh
77 hello.sh
[root@host myshell]# wc -l hello.sh
3 hello.sh
[root@host myshell]# wc -w hello.sh
10 hello.sh

[root@host myshell]# wc  hello.sh 
 3 10 77 hello.sh

 [root@host myshell]# cp hello.sh nihao.sh     
[root@host myshell]# ls
hello.sh  nihao.sh

[root@host myshell]# wc -w /root/myshell/*
 10 /root/myshell/hello.sh
 10 /root/myshell/nihao.sh
 20 total

[root@host myshell]# wc -w hello.sh nihao.sh
 10 hello.sh
 10 nihao.sh
 20 total

 [root@host myshell]# wc /root/myshell/*  
  3  10  77 /root/myshell/hello.sh
  3  10  77 /root/myshell/nihao.sh
  6  20 154 total

mv oldname newname : 重命名文件或移动文件

rm  命令可以删除一个目录中的一个或多个文件或目录,也可以将某个目录及其下属的所有文件及其子目录均删除掉。对于链接文件,只是删除整个链接文件,而原有文件保持不变。

-d:直接把欲删除的目录的硬连接数据删除成0,删除该目录;
-f:强制删除文件或目录;
-i:删除已有文件或目录之前先询问用户;
-r或-R:递归处理,将指定目录下的所有文件与子目录一并处理;
--preserve-root:不对根目录进行递归操作;
-v:显示指令的详细执行过程。

[root@host ~]# rm  -r /root/test      删除一个文件询问一次
rm: descend into directory `/root/test'? y
rm: remove regular file `/root/test/20171026.txt'? n
rm: descend into directory `/root/test/aaa.txt'? n

[root@host test]# rm  -r -f /root/test     直接删除没废话

 [root@host ~]# cp -r tmpdata test        //将tmpdata目录下的所有文件拷贝到test文件

grep(global search regular expression(RE) and print out the line,全面搜索正则表达式并把行打印出来)是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹配的行打印出来。

在一个或者多个文件中搜索一个单词,命令会返回一个包含搜索单词的文本行:
[root@host myshell]# grep 'name' hello.sh
name="xiaoma"
echo "hello,my name is  ${name},I'm ${age} years old."
[root@host myshell]# grep 'is' hello.sh   
echo "hello,my name is  ${name},I'm ${age} years old."
[root@host myshell]# grep 'year' hello.sh  
echo "hello,my name is  ${name},I'm ${age} years old."

[root@host myshell]# grep 'year' hello.sh nihao.sh
hello.sh:echo "hello,my name is  ${name},I'm ${age} years old."
nihao.sh:echo "hello,my name is  ${name},I'm ${age} years old."

输出除之外的所有行 -v 选项:

[root@host myshell]# grep -v 'year' hello.sh nihao.sh
hello.sh:name="xiaoma"
hello.sh:age=100
nihao.sh:name="xiaoma"
nihao.sh:age=100

多级目录递归搜索,下例中 .代表当前目录

[root@host myshell]# grep 'year' . -R -n         
./shellson/hello.sh:3:echo "hello,my name is  ${name},I'm ${age} years old."
./nihao.sh:3:echo "hello,my name is  ${name},I'm ${age} years old."
./hello.sh:3:echo "hello,my name is  ${name},I'm ${age} years old."

 find命令用来在指定目录下查找文件

[root@host myshell]# find /root -name hello.sh
/root/myshell/shellson/hello.sh
/root/myshell/hello.sh

file somefile: 得到文件类型

[root@host myshell]# file hello.sh
hello.sh: ASCII text

read var: 提示用户输入,并将输入赋值给变量

[root@host myshell]# cat read.sh
read varname
echo "my name is ${varname}"
[root@host myshell]# sh read.sh
xiaozhang
my name is xiaozhang

read -a arrayname 把单词清单读入arrayname的数组里

[root@host myshell]# cat read.sh
read -a namearr
echo "they are ${namearr[0]}, ${namearr[1]},${namearr[2]},${namearr[3]}"
[root@host myshell]# sh read.sh
zhang wang sun cherry
they are zhang, wang,sun,cherry

输入赋值多个变量

[root@host myshell]# cat read.sh   
read  name age
echo "my name is ${name},i'm ${age} years old!"
[root@host myshell]# sh read.sh 
xiaoma 50
my name is xiaoma,i'm 50 years old!

 sort命令 将文件进行排序,并将排序结果标准输出。

[root@host tmpdata]# sort -r mm.txt    逆序
zhansan 80 500
tianyongtao 100 80
lisi 1000 36
[root@host tmpdata]# sort  mm.txt    顺序
lisi 1000 36
tianyongtao 100 80
zhansan 80 500
[root@host tmpdata]# sort -n mm.txt 顺序
lisi 1000 36
tianyongtao 100 80
zhansan 80 500

uniq命令用于报告或忽略文件中的重复行,一般与sort命令结合使用。

expr: 进行数学运算

basename 返回文件名

[root@host myshell]# basename /root/myshell/hello.sh
hello.sh

dirname 返回路径
[root@host myshell]# dirname /root/myshell/hello.sh        
/root/myshell

head 返回前n行

[root@host myshell]# head -2 hello.sh
name="xiaoma"
age=100

tail返回尾部n行
[root@host myshell]# tail -2 hello.sh    
age=100
echo "hello,my name is  ${name},I'm ${age} years old."

排序后取第一行

[root@host myshell]# cat hello.sh         
name="xiaoma"
age=100
echo "hello,my name is  ${name},I'm ${age} years old."
[root@host myshell]# sort hello.sh |head -1
age=100

sed是一种流编辑器,它是文本处理中非常中的工具,能够完美的配合正则表达式使用,功能不同凡响。处理时,把当前处理的行存储在临时缓冲区中,称为“模式空间”(pattern space),接着用sed命令处理缓冲区中的内容,处理完成后,把缓冲区的内容送往屏幕。接着处理下一行,这样不断重复,直到文件末尾。文件内容并没有 改变,除非你使用重定向存储输出。Sed主要用来自动编辑一个或多个文件;简化对文件的反复操作;编写转换程序等。

AWK是一种处理文本文件的语言,是一个强大的文本分析工具。

文字闪动

[root@host logs]# echo -e "33[37;31;5mHello world...33[39;49;0m"     
Hello world...

 管道 (|) 将一个命令的输出作为另外一个命令的输入。

[root@host myshell]# grep 'name' hello.sh|wc -l
2

重定向:将命令的结果输出到文件,而不是标准输出(屏幕)。
> 写入文件并覆盖旧文件
>> 加到文件的尾部,保留旧文件内容。

反短斜线:使用反短斜线可以将一个命令的输出作为另外一个命令的一个命令行参数。

-mtime<24小时数>:查找在指定时间曾被更改过的文件或目录,单位以24小时计算;
-name<范本样式>:指定字符串作为寻找文件或目录的范本样式;

[root@host ~]# wc -l `find /root/myshell -mtime -1 -type f -print`
  3 /root/myshell/shellson/hello.sh
  2 /root/myshell/read.sh
  3 /root/myshell/nihao.sh
  3 /root/myshell/hello.sh
 11 total

原文地址:https://www.cnblogs.com/playforever/p/9154200.html