This is going to be a collection of bash code snippets:
-
Check if the user running the script is root:
# make sure we're running as root
if [ `id -u` != 0 ]; then { echo "Sorry, must be root. Exiting..."; exit; } fi
if (( $? )); then
&nbs p; {
&nbs p; &nbs p; &nbs p; echo "could not executed successfully";
&nbs p; &nbs p; &nbs p; exit;
&nbs p; }
fi;
# Check for proper number of command line args.
EXPECTED_ARGS=1
E_BADARGS=65
if [ $# -ne $EXPECTED_ARGS ]
then
echo "Usage: `basename $0` {arg}"
exit $E_BADARGS
fi
VALUES=("value1" "value2" "value3" "..." "valueN")
for ((i=0; i<${#VALUES[@]}; i++))
do
echo ${VALUES[$i]}
done
- sandip's blog
- Login or register to post comments
Comments
cat <<\EOF
The answer is '$foo'!
EOF
The backslash before the EOF makes the here document a literal one.
"$!" will return pid of last background process.
"$$" will return current process pid.
To kill all processes started by the main script:
pkill -g <PID>
SCRIPT_NAME=`basename $0` -p $$ | grep $SCRIPT_NAME | awk '{print $9}')
SCRIPT_PATH=$(/usr/sbin/lsof
SCRIPT_DIR_PATH=`dirname $SCRIPT_PATH`
echo "Name of script: $SCRIPT_NAME"
echo "Path to script: $SCRIPT_PATH"
echo "Path to script directory: $SCRIPT_DIR_PATH"
echo "Script containing directory: ${SCRIPT_DIR_PATH##*/}"
PID_FILE=/var/run/`basename $0`.pid
p; then p; &nbs p; &nbs p; MYPID=`head -n 1 "$PID_FILE"` p; &nbs p; &nbs p; TEST_RUNNING=`ps -p ${MYPID} | grep ${MYPID}`
p; if [ -z "${TEST_RUNNING}" ] p; &nbs p; &nbs p; then p; &nbs p; &nbs p; &nbs p; echo "PID file exists but PID [$MYPID] is not running... creating new PID file [$PID_FILE]" p; &nbs p; &nbs p; &nbs p; echo $$ > "$PID_FILE" p; else p; &nbs p; &nbs p; echo "`basename $0` is already running [${MYPID}]... quitting" p; &nbs p; &nbs p; exit -1 p; echo "`basename $0` not running... creating new PID file [$PID_FILE]" p; echo $$ >> "$PID_FILE"
# Check if the PID is running
if [ -f "$PID_FILE" ]
&nbs
&nbs
&nbs
&nbs
&nbs
&nbs
&nbs
&nbs
&nbs
&nbs
fi
else
&nbs
&nbs
fi
Merge corresponding lines of one or more files into vertical columns, separated by tab.
$ paste merge1.txt merge2.txt > output.txt
$ echo "Title goes here" | cat - test.txt > test.txt.new
or with here:
$ cat - test.txt <<<"Title goes here" > test.txt.new
DEFAULT=${1:-<value>}
$ date +%Y%m%d -d "2008-01-15 +1 month -15 days"