shell脚本:基础知识:组合ls 命令、sort 命令和head 命令

shell脚本:基础知识:组合ls 命令、sort 命令和head 命令


示例:显示前 10 个最大的文件。组合ls 命令、sort 命令和head 命令。

◆如何创建shell脚本(一):基础知识

首先,尝试从终端运行它。
示例:我想显示前 10 个最大的文件。 组合 ls 命令、sort 命令和 head 命令。

$ ls -l | sort -k 5 -nr | head []
-rw-r--r--@  1 yas  prof   67739  5 10 18:03 shinjo-picture-2016-05-10.jpg
-rw-------@  1 yas  prof   53811  5 10 18:02 shinjo-picture-2016-05-10-b.jpeg
-rw-r--r--@  1 yas  prof   21550  5  9 21:07 audio-sampling.png
-rw-r--r--@  1 yas  prof   14523  5  9 22:47 audio-sampling-2.png
drwx------  75 yas  prof    8192  5 20  2014 _OLD_HOME_
drwxr-xr-x   4 yas  prof    6144  3 23 14:53 dot
drwx------  11 yas  prof    5120  4 25 17:07 Desktop
-rw-r--r--   1 yas  prof    4492  4 26 18:29 literacy-a3.txt
-rw-r--r--   1 yas  prof    4226  4 19 17:53 literacy-a2.txt
-rw-r--r--   1 yas  prof    4213  4 19 17:52 literacy-a2.txt~
$ []

使用 echo 命令、history 命令或复制粘贴功能将您在终端中键入的结果保存到文件中。 在下面的示例中,echo 命令用于创建
文件。

$ []
$ ls -l | sort -k 5 -nr | head 
$ echo 'ls -l | sort -k 5 -nr | head' > ls-size10 
$ cat ls-size10
ls -l | sort -k 5 -nr | head
$ ls -l ls-size10 
-rw-r--r--  1 yas  prof  29  6 22 17:46 ls-size10
$ []

使用编辑器删除不需要的部分后,您的 shell 脚本就完成了。 在此示例中,shell 脚本 ls-size10 是完整的,因为没有不必要的部分。 要运行已完成的 shell 脚本,请将文件名指定为 bash 命令的参数,如下所示。

$ bash ls-size10
-rw-r--r--@  1 yas  prof   67739  5 10 18:03 shinjo-picture-2016-05-10.jpg
-rw-------@  1 yas  prof   53811  5 10 18:02 shinjo-picture-2016-05-10-b.jpeg
-rw-r--r--@  1 yas  prof   21550  5  9 21:07 audio-sampling.png
-rw-r--r--@  1 yas  prof   14523  5  9 22:47 audio-sampling-2.png
drwx------  75 yas  prof    8192  5 20  2014 _OLD_HOME_
drwxr-xr-x   4 yas  prof    6144  3 23 14:53 dot
drwx------  11 yas  prof    5120  4 25 17:07 Desktop
-rw-r--r--   1 yas  prof    4492  4 26 18:29 literacy-a3.txt
-rw-r--r--   1 yas  prof    4226  4 19 17:53 literacy-a2.txt
-rw-r--r--   1 yas  prof    4213  4 19 17:52 literacy-a2.txt~
$ []

您不再需要输入诸如“ls -l | sort -k 5 -nr | head”之类的长命令。 此外,您不再需要记住排序命令的选项。
◆如何创建shell脚本(2):“#!”和chmod +x
每次要运行 shell 脚本时都必须输入“bash”,这可能很烦人。 在这种情况下,请执行以下操作:
使用文本编辑器将“#!/bin/bash”添加到第一行。

$ emacs ls-size10 
$ cat ls-size10 
#!/bin/bash
ls -l | sort -k 5 -nr | head
$ []

使用 chmod +x 添加可执行属性

$ ls -l ls-size10 
-rw-r--r--  1 yas  prof  41  6 22 17:49 ls-size10
$ chmod +x ls-size10 
$ ls -l ls-size10 
-rwxr-xr-x  1 yas  prof  41  6 22 17:49 ls-size10
$ []

您可以通过指定文件名来运行它。 文件名可以使用“./”作为前缀,以明确表明它们是相对路径名,或者使用“~/”作为前缀,以表明它们位于主目录中。)。

$ ./ls-size10 
-rw-r--r--@  1 yas  prof   67739  5 10 18:03 shinjo-picture-2016-05-10.jpg
-rw-------@  1 yas  prof   53811  5 10 18:02 shinjo-picture-2016-05-10-b.jpeg
-rw-r--r--@  1 yas  prof   21550  5  9 21:07 audio-sampling.png
-rw-r--r--@  1 yas  prof   14523  5  9 22:47 audio-sampling-2.png
drwx------  75 yas  prof    8192  5 20  2014 _OLD_HOME_
drwxr-xr-x   4 yas  prof    6144  3 23 14:53 dot
drwx------  11 yas  prof    5120  4 25 17:07 Desktop
-rw-r--r--   1 yas  prof    4492  4 26 18:29 literacy-a3.txt
-rw-r--r--   1 yas  prof    4226  4 19 17:53 literacy-a2.txt
-rw-r--r--   1 yas  prof    4213  4 19 17:52 literacy-a2.txt~
$ ~/ls-size10 []
-rw-r--r--@  1 yas  prof   67739  5 10 18:03 shinjo-picture-2016-05-10.jpg
-rw-------@  1 yas  prof   53811  5 10 18:02 shinjo-picture-2016-05-10-b.jpeg
-rw-r--r--@  1 yas  prof   21550  5  9 21:07 audio-sampling.png
-rw-r--r--@  1 yas  prof   14523  5  9 22:47 audio-sampling-2.png
drwx------  75 yas  prof    8192  5 20  2014 _OLD_HOME_
drwxr-xr-x   4 yas  prof    6144  3 23 14:53 dot
drwx------  11 yas  prof    5120  4 25 17:07 Desktop
-rw-r--r--   1 yas  prof    4492  4 26 18:29 literacy-a3.txt
-rw-r--r--   1 yas  prof    4226  4 19 17:53 literacy-a2.txt
-rw-r--r--   1 yas  prof    4213  4 19 17:52 literacy-a2.txt~
$ []

◆如何创建shell脚本(三):~/bin

一旦完成的脚本被放置在 ~/bin 中,它就可以像任何其他命令(ls、cp、emacs 等)一样执行。

$ mkdir ~/bin 
$ ls-size10 
-bash: ls-size10: command not found
$ mv ls-size10 ~/bin 
$ ls -l ~/bin/ls-size10 
-rwxr-xr-x  1 yas  prof  41  6 22 17:49 /home/prof/yas/bin/ls-size10
$ ls-size10 
-rw-r--r--@  1 yas  prof   67739  5 10 18:03 shinjo-picture-2016-05-10.jpg
-rw-------@  1 yas  prof   53811  5 10 18:02 shinjo-picture-2016-05-10-b.jpeg
-rw-r--r--@  1 yas  prof   21550  5  9 21:07 audio-sampling.png
-rw-r--r--@  1 yas  prof   14523  5  9 22:47 audio-sampling-2.png
drwx------  75 yas  prof    8192  5 20  2014 _OLD_HOME_
drwxr-xr-x   4 yas  prof    6144  3 23 14:53 dot
drwx------  11 yas  prof    5120  4 25 17:07 Desktop
-rw-r--r--   1 yas  prof    4492  4 26 18:29 literacy-a3.txt
-rw-r--r--   1 yas  prof    4226  4 19 17:53 literacy-a2.txt
-rw-r--r--   1 yas  prof    4213  4 19 17:52 literacy-a2.txt~
$ []

即使按原样输入文件名,也无法执行主目录中的“ls-size10”。 用mv命令移动到“~/bin/”后,可以用“ls-size10”执行。

◆如何创建shell脚本(4):参数

ls-size10 只能显示当前工作目录(.)。 我想显示其他目录。 另外,我想将数字更改为 5 或 20,而不是前 10 个。 将 shell 脚本命名为“lss”。

$ cd ~/bin 
$ ls -l ls-size10 
-rwxr-xr-x  1 yas  prof  41  6 22 17:49 ls-size10
$ ls -l lss 
ls: lss: No such file or directory
$ cp ls-size10 lss 
$ ls -l lss 
-rwxr-xr-x  1 yas  prof  41  6 22 17:54 lss
$ emacs lss 
$ cat lss 
#!/bin/bash
ls -l $2 | sort -k 5 -nr | head -$1
$ []

(x 位在 cp 时设置,因此不需要 chmod +x。)
在 ls-size10 中,ls 没有参数,但 lss 给出“$2”。 “$2”表示赋予命令的第二个参数。 另外,-$1 被给予 head。
尝试运行您创建的 shell 脚本“lss”。

$ cd []
$ pwd []
/home/prof/yas
$ lss 5 /bin []
-r-xr-xr-x  1 root  wheel  1315248  2  6  2014 ksh
-r-xr-xr-x  1 root  wheel  1228416  5 17 10:33 sh
-r-xr-xr-x  1 root  wheel  1228336  5 17 10:33 bash
-rwxr-xr-x  1 root  wheel   530320  2  6  2014 zsh
-rwxr-xr-x  2 root  wheel   357984  2  6  2014 tcsh
$ lss 3 /usr/bin []
-r-xr-xr-x   1 root   wheel  11738080  5 17 10:33 emacs
-rwxr-xr-x   1 root   wheel  10121104  5 17 10:33 php
-r-xr-xr-x   1 root   wheel   5308832  2  6  2014 parl5.16
$ lss 10 []
-rw-r--r--@  1 yas  prof   67739  5 10 18:03 shinjo-picture-2016-05-10.jpg
-rw-------@  1 yas  prof   53811  5 10 18:02 shinjo-picture-2016-05-10-b.jpeg
-rw-r--r--@  1 yas  prof   21550  5  9 21:07 audio-sampling.png
-rw-r--r--@  1 yas  prof   14523  5  9 22:47 audio-sampling-2.png
drwx------  75 yas  prof    8192  5 20  2014 _OLD_HOME_
drwxr-xr-x   4 yas  prof    6144  3 23 14:53 dot
drwx------  11 yas  prof    5120  4 25 17:07 Desktop
-rw-r--r--   1 yas  prof    4492  4 26 18:29 literacy-a3.txt
-rw-r--r--   1 yas  prof    4226  4 19 17:53 literacy-a2.txt
-rw-r--r--   1 yas  prof    4213  4 19 17:52 literacy-a2.txt~
$ []

◆用于引用 bash 脚本参数的 Shell 变量

在 shell 脚本中,参数可以引用为 $1,$2,…
$* 允许您引用所有参数。
$# 告诉您参数的数量。
可以使用shift命令来移动参数。
(可以使用 set 命令分配参数。通常不这样做。)

$ set a b c []
$ echo $1 $2 []
a b
$ echo $* []
a b c
$ shift []
$ echo $1 $2 []
b c
$ echo $* []
b c
$ []

◆bash评论

Bash 允许您使用“#”字符编写注释。 bash 忽略“#”字符直到行尾。

$ echo a b # c d [←]
a b
$ # echo a b c [←]
$ []

◆Bash 命令替换

Bash 允许您使用一个命令的标准输出作为另一个命令的参数。

$ date +%A []

$ echo Today is `date +%A` . []
Today is  .
$ []

在此示例中,执行反引号 () 中的部分,并将结果用作 echo 命令的参数。 您还可以使用“$(command)”形式来代替反引号 ()。

$ echo Today is $(date +%A) . []
Today is .
$ []

在计算机上,引号 'xxx'' 和反引号 xxx` 是不同的。
反引号也常用于 shell 脚本中。

$ tex=file1.tex []
$ basename file1.tex .tex []
file1
$ basename $tex .tex []
file1
$ base=`basename $tex .tex` []
$ echo $base []
file1
$ []
◆~/.bashrc

当 bash 在 iTerm 中新运行时或(远程)登录期间,bash 会自动运行名为 ~/.bashrc 的文件中的程序。 在 ~/.bashrc 中,执行以下操作:
设置环境变量(PATH、PAGER、EDITOR 等)。
设置 shell 变量(PS1 等)。
定义命令别名(稍后描述)。

◆~/.bashrc 应仔细编辑

编辑 ~/.bashrc 后,最好检查是否可以毫无问题地打开 iTerm 窗口。

如果 ~/.bashrc 有问题并且 iTerm 窗口打不开,请使用以下方法修复它。

  • 在 Dock 中使用 Emacs 打开并修复它。
  • 运行Windows并在其编辑器中进行修改。
  • 使用 MacOSX 附带的终端程序“Terminal.app”代替 iTerm。
    运行(/Applications/Utilities/Terminal.app)。 顶部菜单栏
    单击“Shell”并选择“新命令”。 在“命令:”中输入 bash,例如“/bin/csh”或“/bin/zsh”。
    通过指定除 之外的 shell 来执行。 然后运行emacs并修改.bashrc。

标准 ~/.bashrc 如下。

$ cat /usr/local/lib/standard/bashrc-home 
#
# coins standard ~/.bashrc 
#
if [ -f /usr/local/lib/standard/bashrc ]; then
        . /usr/local/lib/standard/bashrc
fi

# add your own code below 
$ []

◆反映~/.bashrc的编辑结果

编辑 ~/.bashrc 对当前运行的 bash 没有影响。
有两种方法可以使您的编辑有效:

  • 使用exit命令等退出当前的bash,并再次打开窗口。
  • 输入“bash”来运行新的 bash。 (如果用exit退出的话,会回到没有更新的bash)
  • 使用“.”命令或“source”命令执行。 (意思相同。您可以选择其中之一。)
$ . ~/.bashrc 
$ source ~/.bashrc 

◆在~/.bashrc中设置shell变量的示例

在作业 16(1) 中,我们使用 shell 变量来创建短表达式。 要使其永久保存,请将其保存到 ~/.bashrc
在下面的示例中,经常通过 ssh 访问的主机被注册为 shell 变量。

$ cat ~/.bashrc 
...
coins=coins.tsukuba.ac.jp
www=www.$coins
icho=icho.u.tsukuba.ac.jp
...
$ []

这使得 ssh 更容易。

$ ssh $www 
$ ssh $icho 

◆环境变量PATH

环境变量PATH用于设置包含shell命令的目录。 另请参见环境变量 PATH。 当您给 shell 命令文件名(相对或绝对路径名)时,shell 就会执行该程序。 例子:

$ /usr/local3/coins/macosx/bin/emacs 
$ /bin/ls 

您可以通过将目录名称注册到环境变量 PATH 中来省略目录名称。 例子:

$ echo $PATH []
:/usr/local3/coins/macosx/bin::/bin:
$ emacs []
$ ls []

◆~/bin

我们建议设置 ~/.bashrc 等,以便将 ~/bin 包含在 PATH 环境变量中。 如果您将自己的程序或 shell 脚本放在 ~/bin 中,则可以在 ~/bin 下使用短文件,而无需指定文件名(无需键入“~/filename”或“./file”)。您现在可以按文件名运行它。
在币中,~/bin默认包含在PATH中,所以不需要自己配置。

bash别名(别名)

■整理文件

◆配额命令

coins 对个人可用于在其主目录下存储文件的磁盘空间有固定的上限。 使用quota命令显示配额上限和当前使用情况。

$ quota -v []
Disk quotas for user yas (uid 1013): 
 Filesystem 1K blocks  quota limit    grace   files  quota limit   grace
      /home    959318      2764800  3072000   23138            0       0

coins 的标准限制是 3 GB (3*1024 KB)。 如果超过此限制,您将无法保存文件。 我什至无法接收电子邮件。 如果容量超过 2.7 GB (2764800 KB) 的 90%,或剩余量少于 10%,系统会发送警告电子邮件,但电子邮件可能不会发送,因此请自行注意。 参见手册 10.8。
上述用户正在使用 959,318 KB。 文件数量为23,138。 文件数量没有上限。

当超过配额限制时,删除不需要的文件。

注意:MacOSX 垃圾箱中的文件也使用磁盘空间。 仅在执行“清空垃圾箱”操作后,使用的字节数才会减少。

◆du命令

如果您想检查单个目录而不是整个主目录的磁盘使用情况,请使用 du 命令。 将要检查的目录名称作为参数给出。 “.”和“~”也可以用作目录名。 当文件数量较多时,du显示时间较长。 如果您想中途退出,请按 ^C (Control+C) 强制退出。

$ du ~
(显示主目录下文件的容量)
$ du .
(显示当前工作目录下文件的容量)
$ du -s .
(-s 只显示总数)
$ du -s -k .
(使用 -k 以 KB 为单位显示)
$ du -s -k *
(显示指定目录的总数(* 表示其中的所有内容))
$ du -s -k * | sort -nr | head
(按降序对总计进行排序并显示前 10 行)


你可能感兴趣的:(linux,运维,Linux,运维)