shell-script

記錄板端使用到的shell script一些方式

shell script

如何執行目錄內所有script (run-parts)

  • 使用run-parts可以參數給shell script
1
2
run-parts /etc/ppp/ip-down.d \
--arg="$1" --arg="$2" --arg="$3" --arg="$4" --arg="$5" --arg="$6"
1
2
# follow any symlink to find the real file
REALRESOLVCONF=$(readlink -f /etc/resolv.conf)

檢查檔案是否在存,若不存在則離開script

1
2
# If pppconfig has been removed we are not supposed to do anything.
test -f /usr/sbin/pppconfig || exit 0

測試變數是否為空,若是則離開script

1
2
# If we don't have a provider we have nothing to do.
test -z "$PPP_IPPARAM" && exit 0

busybox command

run-parts 說明

1
2
3
4
5
6
7
8
9
10
11
BusyBox v1.16.1 (2016-02-19 09:03:37 UTC) multi-call binary.

Usage: run-parts [-t] [-l] [-a ARG] [-u MASK] DIRECTORY

Run a bunch of scripts in DIRECTORY

Options:
-t Print what would be run, but don't actually run anything
-a ARG Pass ARG as argument for every program
-u MASK Set the umask to MASK before running every program
-l Print names of all matching files even if they are not executable
1
2
3
4
5
6
7
8
Usage: readlink [-fnv] FILE

Display the value of a symlink

Options:
-f Canonicalize by following all symlinks
-n Don't add newline
-v Verbose

shell test 用法

  • -x
    • True if file exists and is executable
    • [ -x .] : 成功回傳0,其它則為非0
1
2
3
4
DAEMON=/usr/sbin/ez-ipupdate
NAME=ez-ipupdate

[ -x $DAEMON ] || exit 0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
test expression

[ expression ]
The test utility evaluates the expression and, if it evaluates to true, returns a zero (true) exit status; otherwise it returns 1 (false). If there is no expression,
test also returns 1 (false).

All operators and flags are separate arguments to the test utility.

The following primaries are used to construct expression:

-b file True if file exists and is a block special file.

-c file True if file exists and is a character special file.

-d file True if file exists and is a directory.

-e file True if file exists (regardless of type).

-f file True if file exists and is a regular file.

-g file True if file exists and its set group ID flag is set.

-h file True if file exists and is a symbolic link.

-k file True if file exists and its sticky bit is set.

-n string True if the length of string is nonzero.

-p file True if file is a named pipe (FIFO).

-r file True if file exists and is readable.

-s file True if file exists and has a size greater than zero.

-t file_descriptor
True if the file whose file descriptor number is file_descriptor is open and is associated with a terminal.

-u file True if file exists and its set user ID flag is set.

-w file True if file exists and is writable. True indicates only that the write flag is on. The file is not writable on a read-only file system even if this
test indicates true.

-x file True if file exists and is executable. True indicates only that the execute flag is on. If file is a directory, true indicates that file can be
searched.

-z string True if the length of string is zero.

-L file True if file exists and is a symbolic link. This operator is retained for compatibility with previous versions of this program. Do not rely on its exis‐
tence; use -h instead.

-O file True if file exists and its owner matches the effective user id of this process.

-G file True if file exists and its group matches the effective group id of this process.

-S file True if file exists and is a socket.

file1 -nt file2
True if file1 and file2 exist and file1 is newer than file2.

file1 -ot file2
True if file1 and file2 exist and file1 is older than file2.

file1 -ef file2
True if file1 and file2 exist and refer to the same file.

string True if string is not the null string.

s1 = s2 True if the strings s1 and s2 are identical.

s1 != s2 True if the strings s1 and s2 are not identical.

s1 < s2 True if string s1 comes before s2 based on the ASCII value of their characters.

s1 > s2 True if string s1 comes after s2 based on the ASCII value of their characters.

n1 -eq n2 True if the integers n1 and n2 are algebraically equal.

n1 -ne n2 True if the integers n1 and n2 are not algebraically equal.

n1 -gt n2 True if the integer n1 is algebraically greater than the integer n2.

n1 -ge n2 True if the integer n1 is algebraically greater than or equal to the integer n2.

n1 -lt n2 True if the integer n1 is algebraically less than the integer n2.

n1 -le n2 True if the integer n1 is algebraically less than or equal to the integer n2.

These primaries can be combined with the following operators:

! expression True if expression is false.

expression1 -a expression2
True if both expression1 and expression2 are true.

expression1 -o expression2
True if either expression1 or expression2 are true.

(expression) True if expression is true.

The -a operator has higher precedence than the -o operator.

busybox config

目前embedded是以ash為主,但也新增相容bash部分

1
2
3
4
5
6
7
8
9
10
11
12
13
14
    Choose your default shell (ash)  ---> 
--- ash
[*] bash-compatible extensions
[*] Job control
[*] alias support
[*] Builtin getopt to parse positional parameters
[*] Builtin version of 'echo'
[*] Builtin version of 'printf'
[*] Builtin version of 'test'
[*] 'command' command to override shell builtins
[ ] Check for new mail on interactive shells
[*] Optimize for size instead of speed
[*] Pseudorandom generator and $RANDOM variable
[*] Expand prompt str

其它

如何取得script自已的pid

  • 使用$$取得pid

  • t1.sh

    1
    2
    3
    4
    5
    6
    7
    8
    9
    #!/bin/bash


    echo $$
    for((i=0; i<11; i=i+1))
    do
    echo $i
    sleep 1
    done
  • [root@localhost tt]# ./t1.sh

    1
    2
    3
    4
    5
    6
    18154
    0
    1
    2
    3
    4
  • check
    t1.sh pid為18154

    1
    2
    3
    root     18154 18056  0 11:54 pts/2    00:00:00 /bin/bash ./t1.sh
    root 18158 18154 0 11:54 pts/2 00:00:00 sleep 1
    root 18159 16537 0 11:54 pts/1 00:00:00 ps -ef