Shell scripts Getting a full - a brief summary

Hello

We met again ha ha ha ha ha ha

This give us some details about a series of automated operation and maintenance of Shell

Note: This is only write about Shell commands and scripts to explain.

Case 1

vim ping.sh
#!/bin/bash # 明确声明用哪个解释器执行
ping -c1 wwww.baidu.com &>/dev/null && echo "www.baidu.com is ok !" 
# 表示当我 成功的Ping 通www.baidu.com 返回给我 www.baidu.com is ok !
命令讲解
# && 表示 &&左边的命令(命令1)返回真(即返回0,成功被执行)后,&&右边的命令(命令2)才能够被执行
# ping -c1 wwww.baidu.com 成功执行后 才会返回 www.baidu.com is ok ! 如果不成功 则不返回。

Case 2

ping -c1 wwww.baidu.com ; echo "www.baidu.com is ok !" 
# 表示要ping www.baidu.com 和 echo "www.baidu.com is ok !"
命令讲解
# ;表示分隔符 不具有逻辑判断意义 即使前面的不成功 后面的也依旧执行。

Case 3

#!/bin/bash
ping -c4 114.114.114.114 && echo "114.114.114.144 is ok" || echo " 114.114.114.114 is not o
k!"
命令讲解
# 表示当我 成功的Ping通 www.baidu.com 返回给我 www.baidu.com is ok 如果不成功 则返回 114.114.114.114 is not ok
# || 与 && 相反 如果前面的不成功 则执行命令

Case 4

#!/bin/bash
ping www.baidu.com && echo "success"
/usr/bin/python <<EOF
print("hello  This is Python")
EOF 
命令解释
# 如上面 我们在一个Shell程序里面指定了bash作为解释器
# 那么如果我想要在脚本里面用到Python语言进行执行 那么我就需要制定Python解释器
# 如下
/usr/bin/python <<-EOF
print("hello  This is Python")
EOF  # 将print("hellow This is Python 指向/usr/bin/python")
# -表示忽略EOF的缩进问题

Expansion of executing some Shell

bash bash.sh # Create a Sub Shell Shell is the implementation of sub-Shell
./bash.sh # Create a Sub Shell Shell is the implementation of sub-Shell
. bash.sh # Shell in the implementation of the current
source bash.ch # in the current Shell executed

Some knowledge of Shell's

/etc/profile
/etc/bashrc  这两个都是系统级Shell
--------------------------------
.bash_profile
.bashrc 这两个是用户级Shell
-------------------------------
.bash_logout   
.bash_history 这两个是推出前会执行的Shell
总结: 登录Shell 执行前四个 退出Shell执行后两个

如果你是bash登录的 那么会执行以下
/etc/profile
/etc/bashrc 
.bash_profile
.bashrc
如果你是Nologin 那么会执行以下
/etc/bashr
.bashrc
如果想体验systemctl补齐功能 请 安装 yum -y install bash-completion
-------------------------------------------------------
如果想以cat的内容作为追加
cat <<EOF >1.txt
123
456
EOF
如此一来 内容便可进行覆盖

Tee pipeline use

cat /etc/sysconfig/network-scripts/ifcfg-ens33 | tee 2.txt 
# 此时会将内容显示出来 在进行追加 由此一来 常用tee管道
Published 15 original articles · won praise 22 · views 2821

Guess you like

Origin blog.csdn.net/Mint_Alone/article/details/104296438