set問題詢問 - Linux
![Hedda avatar](/img/woman-glasses.jpg)
By Hedda
at 2017-09-09T19:09
at 2017-09-09T19:09
Table of Contents
各位前輩打擾了
想在這請教兩個問題
第一個是有關set的用法
不太懂的是關於set --
查詢之後似乎是說:
set -- tells set to stop accepting flags and to stop outputting all the
variables when not passed any argument
單純set -- 是的確不會print all variables
但當有參數時,例如: set -- `ls`
似乎還是會指定argument給positional parameter
所以來問一下關於set --的正確用法
以及確認自己有沒有弄錯的地方
另外一個問題是以下的shell script中:
#!/bin/sh
# killbyname.sh: Kills a process by name -- but only a single instance
#
set -- `ps -u $LOGNAME | grep “ $1$”` # $1 here is different from
if [ $# -eq 4 ] ; then
kill -KILL $1 # the one here
else
echo “Either process $1 not running or more than one instance running”
fi
把它命名為killbyname.sh
並如下執行:
$ sh
$ sh
$ killbyname.sh sh
為什麼會說$1會是sh的PID,且在調用set之後script argument不再可見?
照理來說不是一樣找到帶sh的行 並重新用set指定給positional parameter
然後成功kill掉sh嗎?
希望能提供詳細的解釋
在這請教前輩們以上問題 再拜託了 非常感謝
以下是原文圖片:
http://imgur.com/a/aj5XH
http://imgur.com/a/Jc6Ux
http://imgur.com/a/FkBPr
--
想在這請教兩個問題
第一個是有關set的用法
不太懂的是關於set --
查詢之後似乎是說:
set -- tells set to stop accepting flags and to stop outputting all the
variables when not passed any argument
單純set -- 是的確不會print all variables
但當有參數時,例如: set -- `ls`
似乎還是會指定argument給positional parameter
所以來問一下關於set --的正確用法
以及確認自己有沒有弄錯的地方
另外一個問題是以下的shell script中:
#!/bin/sh
# killbyname.sh: Kills a process by name -- but only a single instance
#
set -- `ps -u $LOGNAME | grep “ $1$”` # $1 here is different from
if [ $# -eq 4 ] ; then
kill -KILL $1 # the one here
else
echo “Either process $1 not running or more than one instance running”
fi
把它命名為killbyname.sh
並如下執行:
$ sh
$ sh
$ killbyname.sh sh
為什麼會說$1會是sh的PID,且在調用set之後script argument不再可見?
照理來說不是一樣找到帶sh的行 並重新用set指定給positional parameter
然後成功kill掉sh嗎?
希望能提供詳細的解釋
在這請教前輩們以上問題 再拜託了 非常感謝
以下是原文圖片:
http://imgur.com/a/aj5XH
http://imgur.com/a/Jc6Ux
http://imgur.com/a/FkBPr
--
Tags:
Linux
All Comments
Related Posts
Ubutu 14.04 network-manager誤刪
![Connor avatar](/img/bee.jpg)
By Connor
at 2017-09-09T08:16
at 2017-09-09T08:16
限制主機對外連線
![Noah avatar](/img/girl4.jpg)
By Noah
at 2017-09-08T16:36
at 2017-09-08T16:36
ubuntu下玩Tomb Raider(R3 1300X + RX560
![Hedy avatar](/img/woman-glasses.jpg)
By Hedy
at 2017-09-05T21:31
at 2017-09-05T21:31
環境變數修改無效
![Carol avatar](/img/cat5.jpg)
By Carol
at 2017-09-05T13:31
at 2017-09-05T13:31
Tunnel常斷線
![Tristan Cohan avatar](/img/cat3.jpg)
By Tristan Cohan
at 2017-09-05T01:33
at 2017-09-05T01:33