網頁

顯示具有 Shell Script 標籤的文章。 顯示所有文章
顯示具有 Shell Script 標籤的文章。 顯示所有文章

2013年3月26日

怎麼再shell script中設定目前所用的環境變數或是改變所在目錄

若沒有做一些特殊安排是做不到的。因為,當我們創造出 child process 時,child process 會繼承其 parent process 的變數與所在的目錄。在這個 child process 只能改到自己的變數與所在目錄而無法影響到其 parent process。

要達到此目的, parent process 要與 child process 有一個溝通的機制。當  child process 要改變變數值時得把要改變的變數及其內容寫到一個講好的地方,讓 parent process 去讀取, 並改變 parent process 的變數。

另一個做法則是寫一個 shell script,然後在 Bourne shell 或 Korn shell 中用 ".",在 C shell 中用 source 去執行那個  shell script。 若此 script 名為 "myscript" :

在 Bourne shell 或 Korn shell 中就用
. myscript

在 C shell 中則用
source myscript

利用這種方式執行 shell script 時,是利用原本的parent process直接執行,而不是 fork 出一個新的 child process 去執行,透過這種方式,就可以保留環境變數或者是改變所在目錄。

總而言之,這個問題算是先天上的限制,也是一種 parent prcoss 和 child process 溝通上的一種機制。

Reference

xargs 將上一個程式輸出當成下一個程式的參數輸入

Origin: xargs 應用介紹

今天介紹的指令是 xargs,這個指令並不常見,卻很實用它的工作是將上一個程式輸出的結果轉成下一個程式的參數輸入

範例:
假設有一個目錄如下 (這個樹狀目錄架構是使用 tree 指令所產生的)

.
|-- Makefile
|-- VERSION
|-- VERSION.bak
|-- inc
|   |-- fork.h
|   |-- main.h
|   `-- main.h.bak
`-- src
    |-- common_lib
    |   |-- dl_math.c
    |   |-- dl_math.c.bak
    |   |-- dl_string.c
    |   `-- dl_string.c.bak
    |-- fork.c
    |-- fork.c.bak
    `-- main.c

這是一個程式開發的專案目錄,如果我想要將所有的*.bak 一次清除,那麼該怎麼做呢? 首先我得先找出所有的 *.bak,可以使用 find 指令

find . -iname "*.bak"

2013年1月28日

使用getopt處理shell腳本的參數

getopt命令並不是bash的內建命令,它是由util-linux包提供的外部命令。相比較bash 的內置命令,getopt不只支持短参-s,還支持--longopt的長参數,甚至支持-longop

#!/bin/bash  

# A small example program for using the new getopt(1) program.
# This program will only work with bash(1)
# An similar program using the tcsh(1) script language can be found
# as parse.tcsh

# Example input and output (from the bash prompt):
# ./parse.bash -a par1 'another arg' --c-long 'wow!*\?' -cmore -b " very long "
# Option a
# Option c, no argument
# Option c, argument `more'
# Option b, argument ` very long '
# Remaining arguments:
# --> `par1'
# --> `another arg'
# --> `wow!*\?'

# Note that we use `"$@"' to let each command-line parameter expand to a
# separate word. The quotes around `$@' are essential!
# We need TEMP as the `eval set --' would nuke the return value of getopt.
TEMP=`getopt -o ab:c:: --long a-long,b-long:,c-long:: \
-n 'example.bash' -- "$@"`

if [ $? != 0 ] ; then echo "Terminating..." >&2 ; exit 1 ; fi

# Note the quotes around `$TEMP': they are essential!
eval set -- "$TEMP"

while true ; do
case "$1" in
-a|--a-long) echo "Option a" ; shift ;;
-b|--b-long) echo "Option b, argument \`$2'" ; shift 2 ;;
-c|--c-long)
# c has an optional argument. As we are in quoted mode,
# an empty parameter will be generated if its optional
# argument is not found.
case "$2" in
"") echo "Option c, no argument"; shift 2 ;;
*) echo "Option c, argument \`$2'" ; shift 2 ;;
esac ;;
--) shift ; break ;;
*) echo "Internal error!" ; exit 1 ;;
esac
done
echo "Remaining arguments:"
for arg do echo '--> '"\`$arg'" ; done

Passing arguments to a shell script

From: Passing arguments to a shell script

Any shell script you run has access to (inherits) the environment variables accessible to its parent shell. In addition, any arguments you type after the script name on the shell command line are passed to the script as a series of variables.

The following parameters are recognized:

$*

Returns a single string (``$1, $2 ... $n'') comprising all of the positional parameters separated by the internal field separator character (defined by the IFSenvironment variable).

$@

Returns a sequence of strings (``$1'', ``$2'', ... ``$n'') wherein each positional parameter remains separate from the others.

$1, $2 ... $n

Refers to a numbered argument to the script, where n is the position of the argument on the command line. In the Korn shell you can refer directly to arguments where n is greater than 9 using braces. For example, to refer to the 57th positional parameter, use the notation ${57}. In the other shells, to refer to parameters with numbers greater than 9, use the shift command; this shifts the parameter list to the left. $1 is lost, while $2 becomes $1, $3 becomes $2, and so on. The inaccessible tenth parameter becomes $9 and can then be referred to.

$0

Refers to the name of the script itself.

$#

Refers to the number of arguments specified on a command line.