cmdref.net - Command Reference

This site is command references for system engineers.

User Tools

Site Tools


Top     SiteMap

Sidebar

Top     SiteMap








Etc


Reference











.

programming:shell:index.html



Programming

Shell Script Cheat Sheet

References

Grammar

Variables

#!/bin/sh
dir=`pwd`
echo $dir


argument

#!/bin/sh
#./test.sh 192.168.0.10 test-server

IP=`echo $1`
HOST=`echo $2`
echo "$IP $HOST"

echo $0
echo $@

$0 : shellname
$@ : all  argument  =  arg1  arg2   arg3  ....
$1 : argument1
$2 : argument2


Conditionals (-f,-d,=)

[ -d "$dir" ] || mkdir $dir
[ ! -d "$dir" ] && mkdir $dir
[ -z "$END" ] && END=`date --date '1day ago' +"%Y%m%d"2359`

-d file  : True if file exists and is a directory.
-f file  : True if file exists and is a regular file.
-z arg   : True if argument is null

string1 = string2  : True if string1 and string2 are equal.
string == pattern  : True if string matches pattern.
string != pattern  : True if string does not match pattern.

A -eq B    :  A is equal  B
A -ne B    :  A is not equal  B
A -gt B    :  A is grater than  B
A -ge B    :  A is grate equal  B
A -lt B    :  A is less than  B
A -le B    :  A is little equal  B

#!/bin/sh

test -f  /tmp/tmp.txt
if [ "$?" -ne "0" ]; then
    echo "ERR : "
else
    echo "SUCCESS : "
fi

#!/bin/sh

if [ -z $1 ]; then
    echo "Error:  argument is null"
    exit 1
fi


Conditionals (if,case)

if statements

if [ "$?" -ne 0 ] ; then
   echo "ERROR : XXXXXX."
   exit 1
fi

if [ "${USER}" != "root" ] ; then
   echo "ERROR : Change root user."
   exit 1
fi

if [ -f /etc/sysconfig/httpd ]; then
        . /etc/sysconfig/httpd
fi

if [ ! -d ${DIR} ]; then
    mkdir -p ${DIR}
fi

and, or
-a and
-o or

if test xxx -a xxx; then
  echo "aaa"
fi

if [ "$?" -eq 0 -a "$a" = 1 ]; then

if test "$?" -eq 0 -a "$a" = 1 ; then

case statements

case "$VAR" in
  [qQ] )
        echo "Quit"
        ;;
  [yY]es | "YES" )
        echo "Yes"
        ;;
  [nN]o | "NO" )
        echo "No"
        ;;
  *) 
      echo "etc"
        ;;
esac


Loops (for,while,until)

for statements

for host in `cat /tmp/hostlist.txt`
do
    scp tmp.sh ${host}:/tmp
    ssh -n ${host} "hostname;sudo su - -c '/tmp/tmp.sh'"
done

#!/bin/sh

FLAG=0

for i in aaa bbb
do
   echo  $i
   if [ "$?" -ne 0 ] ; then
      FLAG=1
      logger "ERR :  $i"
   fi
done

exit $FLAG

H="server1 server2"
date;for i in $H ; do echo "# $i"; echo $i ; done


while statements

while read i
do
    echo ${i} | awk '{ print $1 }' 
    HOST=`echo ${i} | awk '{ print $1 }' `
done < /tmp/hostlist.txt

#!/bin/sh

NUM=1

while [ "${NUM}" -le 5 ]
do
    echo "${NUM}"
    NUM=`expr ${NUM} + 1`
    sleep 1
done

#!/bin/sh

FILE="test.txt"

cat $FILE |grep XXX | while read i
do
  echo $i
done

while : ; do date; /usr/sbin/ntpq -p ; df -h ; sleep 1 ; clear ; done


Function

#!/bin/sh

COMMAND1 () {
    echo "$1"
}

COMMAND1 192.168.0.10


Here Dcoument

#!/bin/sh

COMMAND1 () {
    echo "$1"
}

cat <<MENU
***************************************************************
TEST
---------------------------------------------------------------
   1) COMMAND1
   q) quit
***************************************************************
MENU

echo -n -e "\t >"
read NUM
case ${NUM} in
  1) echo "COMMAND1"
    COMMAND1 $IP
    ;;
  q|Q) echo "quit"
     exit ;;
  *) echo "exit : no selection is missed."
     exit
     ;;
esac

#!/bin/sh

cat << EOT > tmp.txt
  test1
  test2
EOT




Tips

Idempotency

# /usr/bin/id user01 > /dev/null 2>&1  || echo "useradd user01"
# /usr/bin/id user01 > /dev/null 2>&1  || /usr/sbin/useradd user01

# /usr/bin/id user01 > /dev/null 2>&1  && echo "userdel user01"
# /usr/bin/id user01 > /dev/null 2>&1  && /usr/sbin/userdel -r user01

get the row

# cat test.txt
1
2
3
4
5

# sed -n '2p' test.txt
2

# sed -n '$p' test.txt
5

# sed -e '1,4d' test.txt
5

# sed -e '1,3d' test.txt
4
5

# sed -e '3,$d' test.txt
1
2

Delete the line of line breaks only

# cat test.txt
1
2

4
5
#sed '/^ *$/d' test.txt
1
2
4
5


get the column

Operation Command
1st column awk '{ print $1 }' FILE
1,4 column awk '{ print $1 $4 }' FILE
2 column , “:” is filed sepalater awk -F: '{ print $2 }'

#echo "1 2 3 4 5" | awk '{ print $1,$3 }'
1 3
#

#echo "1 2 3 4 5" | awk '{ print $1","$3 }'
1,3
#

#echo "1 2 3 4 5" | awk '{ print $NF }'
5
#

#echo "1 2 3 4 5 6 7 8" | awk '{ {for(i=2;i<NF;i++)printf("%s ",$i) }print($NF) }'
2 3 4 5 6 7 8
#

#echo "1 2 3 4 5" | awk '{print $1 "        " $4 }'
1        4
#

#echo "1 2 3 4 5" | awk '{print $1 "\t" $4 }'
1       4
#

#echo "1 2 : 3 4 : 5" | awk -F: '{ print $2 }'
 3 4
#


mail

cat  test.txt | mail -s "TITLE" test@example.com

#!/bin/bash

mailaddr="user1@example.com, user2@example.com"
cat <<EOM | mail -s "`hostname`: TEST" $mailaddr
Dear testuser

This is test mail


`date`

-----------------------
`cat /tmp/test.txt`

EOM


Database

MySQL

mysql -u root -pPASS << EOF
use mysql;
show tables;
EOF


Change pre tag

sed -e 's/\&/\&amp;/g' -e 's/>/\&gt;/g' -e 's/</\&lt;/g' -e 's/"/\&quot;/g' -e "s/'/\&\#039;/g"  sample.txt > sample.txt

-------------------------
<  :  &lt;
>  :  &gt;
&  :  &amp;
"  :  &quot
'  :  &#039;




Sample




programming/shell/index.html.txt · Last modified: 2017/08/07 by admin