Additional Features for Scripting

1.1 "Daemon-izing" Your Script

Use the following to invoke your script, run it in the background, and still allow yourself to log out:

[maxwell@MaxwellDBA test]$ nohup mydaemonscript 0<&- 1>/dev/null 2>&1 &
[1] 209178
[maxwell@MaxwellDBA test]$ nohup mydaemonscript >>/var/log/myadmin.log 2>&1 <&- &
[2] 209179
[1]   Exit 127                nohup mydaemonscript 0>&- > /dev/null 2>&1
[maxwell@MaxwellDBA test]$ -bash: /var/log/myadmin.log: Permission denied

[2]+  Exit 1                  nohup mydaemonscript >> /var/log/myadmin.log 2>&1 0>&-
[maxwell@MaxwellDBA test]$ 

But what about STDIN? The cleanest way to deal with STDIN is to close the file descriptor. The bash syntax to do that is like a redirect, but with a dash for the filename (0<&- or <&-).

We use the nohup command so that the script is run without being interrupted by a hangup signal when we log off.

1.2 Reusing Code with Includes and Sourcing

[maxwell@MaxwellDBA test]$ sh -x use_prefs.sh
+ source /home/maxwell/shelllearning/test/myprefs.cfg
++ SCRATCH_DIR=/var/tmp
++ IMG_FMT=png
++ SND_FMT=ogg
+ cd /var/tmp
+ echo You prefer png image files
You prefer png image files
+ echo You prefer ogg sound files
You prefer ogg sound files
[maxwell@MaxwellDBA test]$ cat myprefs.cfg
SCRATCH_DIR=/var/tmp
IMG_FMT=png
SND_FMT=ogg
[maxwell@MaxwellDBA test]$ cat use_prefs.sh
#!/bin/bash
#
# use the user prefs
#
source $HOME/shelllearning/test/myprefs.cfg
cd ${SCRATCH_DIR:-/tmp}
echo You prefer $IMG_FMT image files
echo You prefer $SND_FMT sound files
[maxwell@MaxwellDBA test]$ 

1.5 Using Functions: Parameters and Return Values

# define the function:
function max ( )
{ ... }
#
# call the function:
#
max 128 $SIM
max $VAR $CNT
[maxwell@MaxwellDBA test]$ sh -x max 128 $SIM
[maxwell@MaxwellDBA test]$ echo $BIGR

[maxwell@MaxwellDBA test]$ cat max
#!/bin/bash
#cookbook filename: func_max

# define the function:
function max ()
{
  local HIDN
  if [ $1 -gt $2 ]
  then
      BIGR=$1
  else
      BIGR=$2
  fi
  HIDN=5
}
[maxwell@MaxwellDBA test]$ 

1.6 Trapping Interrupts

Use the trap utility to set signal handlers. First, use trap -l (or kill -l) to list the signals you may trap. They vary from system to system:

[maxwell@MaxwellDBA test]$ trap -l
 1) SIGHUP       2) SIGINT       3) SIGQUIT      4) SIGILL       5) SIGTRAP
 6) SIGABRT      7) SIGBUS       8) SIGFPE       9) SIGKILL     10) SIGUSR1
11) SIGSEGV     12) SIGUSR2     13) SIGPIPE     14) SIGALRM     15) SIGTERM
16) SIGSTKFLT   17) SIGCHLD     18) SIGCONT     19) SIGSTOP     20) SIGTSTP
21) SIGTTIN     22) SIGTTOU     23) SIGURG      24) SIGXCPU     25) SIGXFSZ
26) SIGVTALRM   27) SIGPROF     28) SIGWINCH    29) SIGIO       30) SIGPWR
31) SIGSYS      34) SIGRTMIN    35) SIGRTMIN+1  36) SIGRTMIN+2  37) SIGRTMIN+3
38) SIGRTMIN+4  39) SIGRTMIN+5  40) SIGRTMIN+6  41) SIGRTMIN+7  42) SIGRTMIN+8
43) SIGRTMIN+9  44) SIGRTMIN+10 45) SIGRTMIN+11 46) SIGRTMIN+12 47) SIGRTMIN+13
48) SIGRTMIN+14 49) SIGRTMIN+15 50) SIGRTMAX-14 51) SIGRTMAX-13 52) SIGRTMAX-12
53) SIGRTMAX-11 54) SIGRTMAX-10 55) SIGRTMAX-9  56) SIGRTMAX-8  57) SIGRTMAX-7
58) SIGRTMAX-6  59) SIGRTMAX-5  60) SIGRTMAX-4  61) SIGRTMAX-3  62) SIGRTMAX-2
63) SIGRTMAX-1  64) SIGRTMAX
[maxwell@MaxwellDBA test]$ 

[maxwell@MaxwellDBA test]$ cat hard_to_kill.sh
#!/bin/bash -
trap ' echo "You got me! $?" ' ABRT EXIT HUP INT TERM QUIT
trap ' echo "Later... $?"; exit ' USR1
sleep 120
[maxwell@MaxwellDBA test]$ sh -x hard_to_kill.sh
+ trap ' echo "You got me! $?" ' ABRT EXIT HUP INT TERM QUIT
+ trap ' echo "Later... $?"; exit ' USR1
+ sleep 120
^C++ echo 'You got me! 130'
You got me! 130
+ echo 'You got me! 130'
You got me! 130
[maxwell@MaxwellDBA test]$ sh -x hard_to_kill.sh &
[1] 209366
[maxwell@MaxwellDBA test]$ + trap ' echo "You got me! $?" ' ABRT EXIT HUP INT TERM QUIT
+ trap ' echo "Later... $?"; exit ' USR1
+ sleep 120

[maxwell@MaxwellDBA test]$ kill -USR1 %1
[maxwell@MaxwellDBA test]$ User defined signal 1
++ echo 'Later... 138'
Later... 138
++ exit
+ echo 'You got me! 0'
You got me! 0

[1]+  Done                    sh -x hard_to_kill.sh
[maxwell@MaxwellDBA test]$ sh -x hard_to_kill.sh &
[1] 209374
[maxwell@MaxwellDBA test]$ + trap ' echo "You got me! $?" ' ABRT EXIT HUP INT TERM QUIT
+ trap ' echo "Later... $?"; exit ' USR1
+ sleep 120

[maxwell@MaxwellDBA test]$ kill %1
[maxwell@MaxwellDBA test]$ Terminated
++ echo 'You got me! 143'
You got me! 143
+ echo 'You got me! 143'
You got me! 143

[1]+  Exit 143                sh -x hard_to_kill.sh
[maxwell@MaxwellDBA test]$ 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值