這篇文章主要對比一下 source 命令執行shell文件和 ./ping.sh 這種方式執行shell文件的區別。
1. source ping.sh 這個是在當前的shell 中執行 ping.sh 里面的內容的。(source 和 . 是相同的,It has a synonym in . (period))
怎么查看當前的shell呢?
$$ 這個會輸出當前shell的pid, echo $SHELL 這個變量直接輸出當前shell ,我們這里是“/bin/bash”

#!/bin/bash
# 1、ping -c1 -w1 中-c1是指ping的次數,-w是指執行的最后期限,也就是執行的時間,單位為秒
# 2、&>/dev/null 是指標准輸出和錯誤輸出都輸出到/dev/null上,而不在界面上顯示;
# 后面的&&和|| 是與和或得意思,如a&&b||c ,表示a為真,則執行b;否則執行c
for I in `seq 1 254`; do
ping -c1 -w1 192.168.123.$I &>/dev/null && echo "192.168.123.$I is up" || echo "$I down"
done



這個ping.sh在當前的shell中執行的,相當於 ping 命令是一條一條輸入到當前的shell 中的。
參考:
source is a bash shell built-in command that executes the content of the file passed as argument, in the current shell. It has a synonym in . (period).
Syntax
. filename [arguments] source filename [arguments]
------------------------------------------------------------------------------------------------------------------------------
2. ./ping.sh 執行

