Useful Syntax in Shell
#!/bin/bash
#. ./header.sh
#source ./header.sh
echo `date` # print date
printf "Hello, Shell\n"
echo "file:$0";
echo "parameter count: $#";
echo "parameter string: $*";
echo "first parameter:$1";
echo "second parameter:$2";
echo "file content" > filename
## print
user="Bendy Zhang"
age=100
readonly user
echo $user
echo "welcome ${user}"
echo "user length is ${#user}"
echo "cut string: ${user:1:4}"
printf "Hello, %-15s \n" $user # Hello, Bendy Zhang
unset age # you can not unset readonly variable
## if then elif fi
num1=100
num2=100
if test $[num1] -eq $[num2]
then
echo '=='
else
echo '!='
fi
# LOOP
for file in `ls /etc`; do
echo $file
done
for skill in Ada Coffe Action Java; do
echo "I am good at ${skill}Script"
done
int=1
while(( $int<=5 ))
do
echo $int
let "int++"
done
## array
array_name=("bing" "zhang" 100 1)
array_name[0]="Bing"
array_name[3]="str"
echo ${array_name[@]} # print all elements
length=${#array_name[@]} # array length
length=${#array_name[*]} # array length
## expression
val=`expr 2 + 2`
echo "sum: $val"
## File (-e)
FILE=/tmp/foo.txt
if [ ! -f "$FILE" ]
then
echo "File $FILE does not exist"
fi
DIR=/tmp/dir
if [ -d "$DIR" ]
then
cp -a /source/* $DIR
fi
#mkdir -p $DIR # create all directories
chmod +x ./demo.sh
./demo.sh
bash -x demo.sh # debug
-b FILE FILE exists and is block special
-c FILE FILE exists and is character special
-d FILE FILE exists and is a directory
-e FILE FILE exists
-f FILE FILE exists and is a regular file
-g FILE FILE exists and is set-group-ID
-G FILE FILE exists and is owned by the effective group ID
-h FILE FILE exists and is a symbolic link (same as -L)
-k FILE FILE exists and has its sticky bit set
-L FILE FILE exists and is a symbolic link (same as -h)
-O FILE FILE exists and is owned by the effective user ID
-p FILE FILE exists and is a named pipe
-r FILE FILE exists and read permission is granted
-s FILE FILE exists and has a size greater than zero
-S FILE FILE exists and is a socket
-t FD
file descriptor FD is opened on a terminal
-u FILE FILE exists and its set-user-ID bit is set
-w FILE FILE exists and write permission is granted
-x FILE FILE exists and execute (or search) permission is granted
if [ operator FileName ]
then
echo "FileName - Found, take some action here"
else
echo "FileName - Not found, take some action here"
fi
date.sh
#!/bin/bash
DAY=`date '+%m'`
HOUR=`date '+%H'`
HAS_ARGS=false
SERVER="<date server>"
usage() {
echo "USAGE(Example): setDate -d 10 -h 23"
echo "sync date from ${SERVER}..."
ntpdate ${SERVER}
echo "done."
exit 1;
}
while getopts d:h: o; do
case "$o" in
d)
DAY=${OPTARG}
HAS_ARGS=true
;;
h)
HOUR=${OPTARG}
HAS_ARGS=true
;;
esac
done
shift $OPTIND-1
if !(${HAS_ARGS}); then
usage
else
echo "current date: "`date`
d1=`date '+%m'`
d2=`date '+%M%Y'`
d="${d1}${DAY}${HOUR}${d2}"
echo "set date to ${d}..."
date $d
echo "current date: "`date`
fi
./date.sh -d <day> [-h <hour>]
./date.sh // sync date with <date server>
cp -R /source /dist # copy source folder and all chilren to dist folder
cp -R /source/. /dist # copy all files in source folder to dist folder
\cp -f /source/. /dist # copy files with disabled the overwrite hint
auto-sync.cron
0 0 * * * cd "/opt/git-workspace/github.js" && npm run pull:gists && cd "/opt/git-workspace/bndy.net" && git pull && \cp -R -f ../github.js/temp/blog/. ./_posts/ && git add . && git commit -m "auto-sync gists" && git push >/dev/null 2>&1
crontab auto-sync.cron # append the job in auto-sync.cron file to list
crontab -l # list curent user jobs
crontab -e # edit all current user jobs, you can remove or edit the jobs
scp <file to copy> root@<ip>:/<dest folder>
traceroute <ip/domain>