****************************** linux运维常用命令 ****************************** 查看具体端口 ============================ .. code-block:: console $ netstat -tnlp |grep 9100 在后台运行进程 ============================== .. code-block:: console $ nohup ./agent_manager >agent_manager.log 2>&1 & 停止脚本stop_updater.sh的内容 .. code-block:: bash #!/bin/bash kill `ps -ef | grep agent_manager | grep -v grep | awk '{print $2}'` kill `ps -ef | grep node_exporter | grep -v grep | awk '{print $2}'` shell ================================ if.sh -------------------- .. code-block:: shell #!/bin/bash # testing the if statement if pwd # 如果该命令的退出状态码为0, 那么位于then部分的命令就会被执行。 then echo "it worked" fi # testing multiple commands in the then block # 在then语句中列出多条命令 testuser=christine if grep $testuser /etc/passwd then echo "This is my first command in the then block." echo "This is my second command in the then block." echo "I can event put in other commands besides echo:" ls /home/$testuser/*.sh fi echo "We are outside the if statement" # testing the else section # if ... then ... else testuser=NoSuchUser # if grep $testuser /etc/passwd then echo "Thescript files in the home directory of $testuser are:" ls /home/$testuser/*.sh echo else echo "The user $testuser dose not exist on this system." echo fi echo "We are outside the if statement" # testing nested ifs - using elif # elif语句 if grep $testuser /etc/passwd then echo "The user $testuser account exists on this system." echo elif ls -d /home/$testuser/ then echo "The user $testuser has a directory," echo "even though $testuser doesn't have an account." else echo "The user $testuser does not exist on his system" echo "and no directory exists for the $testuser" fi echo "We are outside the nested if statements." mydate.sh -------------------- .. code-block:: shell #!/bin/bash # 代码来自<> 11.9 # calculate the number of days between two dates date1="Jan 1, 2020" date2="May 1, 2020" # 原文是使用expr命令,但这个是过时的语法,参阅: https://github.com/koalaman/shellcheck/wiki/SC2003 time1=$(date -d "$date1" +%s) time2=$(date -d "$date2" +%s) diff=$((time2 - time1)) secondsinday=$((24 * 60 * 60)) days=$((diff / secondsinday)) echo "The difference between $date2 and $date1 is $days days"