指点成金-最美分享吧

登录

Linux中的sh命令的详细解释

佚名 举报

篇首语:本文由小编为大家整理,主要介绍了Linux中的sh命令的详细解释相关的知识,希望对你有一定的参考价值。

参考技术A

  linxu下的sh命令相当于是shell命令语言的解释器。下面由我为大家整理了linux的sh命令的详细解释的相关知识,希望对大家有帮助!

  一、Linux中的sh命令的详细解释

  sh命令是shell命令语言解释器,执行命令从标准输入读取或从一个文件中读取。通过用户输入命令,和内核进行沟通!Bourne Again Shell (即bash)是自由软件基金会(GNU)开发的一个Shell,它是Linux系统中一个默认的Shell。Bash不但与Bourne Shell兼容,还继承了C Shell、Korn Shell等优点。

  语法

  bash [options] [file]

  选项

  -c string:命令从-c后的字符串读取。

  -i:实现脚本交互。

  -n:进行shell脚本的语法检查。

  -x:实现shell脚本逐条语句的跟踪。

  二、Linux中的sh命令的具体例子

  使用-x选项跟踪脚本调试shell脚本,能打印出所执行的每一行命令以及当前状态:

  [root@AY1307311912260196fcZ satools]# sh -x check_ssh_login.sh

  + DEFINE=30

  + cat /var/log/secure

  + awk "/Failed/ ++ip[$(NF-3)] END for (i in ip) print i"="ip[i]"

  ++ cat /root/satools/black.txt

  + for i in "`cat /root/satools/black.txt`"

  ++ echo 121.42.0.16=1427

  ++ awk -F= "print $1" + IP=121.42.0.16

  ++ echo 121.42.0.16=1427

  ++ awk -F= "print $2"

  + NUM=1427

  + "[" 1427 -gt 30 "]"

  + grep 121.42.0.16 /etc/hosts.deny

  + "[" 1 -gt 0 "]"

  + echo sshd:121.42.0.16

  + echo vsftpd:121.42.0.16

  + for i in "`cat /root/satools/black.txt`"

  ++ echo 121.42.0.72=276

  ++ awk -F= "print $1"

  + IP=121.42.0.72

  ++ awk -F= "print $2"

  ++ echo 121.42.0.72=276

  + NUM=276 + "[" 276 -gt 30 "]"

  + grep 121.42.0.72 /etc/hosts.deny

  + "[" 1 -gt 0 "]"

  + echo sshd:121.42.0.72

  + echo vsftpd:121.42.0.72

  三、Linux中对.sh文件的操作命令

  1、创建test.sh文件

  touch test.sh

  2、编辑sh文件

  vi test.sh

  3、保存退出

  敲击esc, 然后输入 :wq ,回车退出

  4、添加可执行权限,当然默认就是可执行的。

  chmod +x test.sh

  5、运行文件

  (1)./test.sh

  (2)sh test.sh

  6、删除文件

  rm test.sh

Linux中的预定义变量

解释:
  主要是Bash中已经定好的变量,名称不能自定义,作用也是固定的

$?
  最后一次执行的命令返回状态,0为成功,非0为失败
$$
  当前进程的进程号
$!
  后台运行的最后一个进程的进程号

例子:
[[email protected] sh]# ls
param_test2.sh param_test.sh
[[email protected] sh]# echo $?
0

[[email protected] sh]# lss
-bash: lss: command not found
[[email protected] sh]# echo $?
127

[[email protected] sh]# echo $$
8466

[[email protected] sh]# find /root -name hello.sh &
[1] 8700
[[email protected] sh]# echo $!
8700
[1]+ Done find /root -name hello.sh





























以上是关于Linux中的sh命令的详细解释的主要内容,如果未能解决你的问题,请参考以下文章