Bash Tips and Pitfalls: Difference between revisions
Jump to navigation
Jump to search
(→Miscellaneous: Get PID of a new process when other processes with same name are already running) |
|||
Line 55: | Line 55: | ||
uniqPID=`echo $oldPID $newPID|sed -e 's/ /\n/g'|sort|uniq -u` |
uniqPID=`echo $oldPID $newPID|sed -e 's/ /\n/g'|sort|uniq -u` |
||
echo $uniqPID |
echo $uniqPID |
||
</source> |
|||
Getting the ''pid'' of a running process |
|||
<source lang="bash"> |
|||
pid=$(pidof -o $$ -o $PPID - o %PPID -x /bin/ssh) |
|||
</source> |
</source> |
||
Revision as of 10:09, 22 September 2009
Tips
Parsing Command-Line Option Parameters
- To ease parsing, pre-parse with executable getopt or Bash/sh built-in getopts, see here for more information and examples.
- To parse option like -value=name ([1])
until [[ ! "$*" ]]; do
if [[ ${1:0:2} = '--' ]]; then
PAIR=${1:2}
PARAMETER=`echo ${PAIR%=*} | tr [:lower:] [:upper:]`
eval P_$PARAMETER=${PAIR##*=}
fi
shift
done
Miscellaneous
Empty a file named filename, keeping the same permission and user/group:
>filename
Print multi-lines text with echo:
$ echo -e "Some text\n...on 2 lines..." # Enable interpretation of backslash escapes (must be quoted!)
Some text
...on 2 lines...
The different ways to extract file size in a Bash script:
SIZE=$(stat -c%s "$FILENAME") # Using stat
SIZE=$(ls -l $FILENAME | awk -F" "'{ print $5 }') # Using ls / awk
SIZE=$(du -b $FILENAME | sed 's/\([0-9]*\)\(.*\)/\1/') # Using du
SIZE=$(cat $FILENAME | wc -c) # Using cat / wc
SIZE=$(ls -l $FILENAME | cut -d " " -f 6) # Using ls / cut
Read the content of a file into an environment variable:
PID=`cat $PIDFILE`
read PID < $PIDFILE
Getting the pid of a new process (when other processes with same name are already running)
oldPID=`pidofproc /usr/bin/ssh`
/usr/bin/ssh -f -N -n -q -D 1080 noekeon
RETVAL=$?
newPID=`pidofproc /usr/bin/ssh`
uniqPID=`echo $oldPID $newPID|sed -e 's/ /\n/g'|sort|uniq -u`
echo $uniqPID
Getting the pid of a running process
pid=$(pidof -o $$ -o $PPID - o %PPID -x /bin/ssh)
Pits
Description | Example |
---|---|
Space! - Don't forget to add spaces whenever necessary, in particular around brace in function definition, or in test conditions for ifs. |
if -space- [ -space- -f /etc/foo -space- ]; then ... |
Quote - Always quote parameters, variables passed to test in if ... then ... else: |
if [ "$name" -eq 5 ]; then ... |
For loops with file - Use simply * to list files in for loops, not `ls *`: |
for file in *; cat "$file"; done # SUCCEEDS, even if white space
for file in `ls *`; cat "$file"; done # FAILS miserably
|
Space variable setting - There must be no space between the variable name and the subsequent equal sign. Also the variable name must not be prefixed with a $ |
srcDir = $1 # WRONG - spaces around = sign
$srcDir=$1 # WRONG - variable name must not have $ prefix
srcDir=$1 # CORRECT
srcDir="$1" # BEST
|
Semi-colon in find - Semi-colon in find commands must be escaped ! |
find . -exec echo {} ; # WRONG - semi-colon not escaped
find . -exec echo {} \; # CORRECT
|