set問題詢問 - Linux

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

--

All Comments