MENU

Shell programming Basic

July 3, 2018 • Read: 348 • Note

Shell programming

Bourne Shell: sh, Bash

$ sh # bash
$ csh # c-shell

Password

$ vi /etc/passwd

Output ! is special (use ')

$ echo 'Hello World!' 
  • \x -> ascii
  • \b -> backspace
  • \e[1; \e[0m -> output with color

Executable

$ chmod 755 hello.sh
$ bash hello .sh

Enter ↩︎ in Windows: ^M$, Linux: $

$ dos2unix hello.sh

History

~/.bash_history will save when exit

$ history 
  • -c : clean history
  • -w save cache to history
$ !n         # repeat n'th command 
$ !!        # repeat the last command
$!ser        # repeat the last command start with "ser"

Alias

$ alias vi='vim'
  • Ctrl + L clear screen
  • Ctrl + U delete string
  • Ctrl + R search command
  • Ctrl + D exit terminal

Redirect output

# > means cover, >> means append
# save both correct and error command into file
$ command >> file  2>&1
$ command  &>> file
# save error output to file2
$ command >> file1  2>> file2 
$ command &>/dev/null     # drop output 

Redirect input

$ wc < hello.sh    # calculate lines, words, chars

if = input file, of = output file bs = char

$ date;dd if=/dev/zero of=/root/testfile bs=1k count=100000;date

grep

  • -i ignore captial or lowercase
  • -n show line number
  • --color=auto show with color

Wildcard characters -> match file name

  • ? any char (only one)
  • * any count char(can be zero)
  • [] any char in [ ] (only one)
  • [-] range
  • [^] logical NO

Variable

All string type in shell

assigin value a=1 cannot contain space between =

environment variable should be captial form

Pre defined variable must not be modified

Environment variable append

$ PATH="PATH":/root/sh

PS1 CONTROL COMMAND

  • $* $@ (The former handle params as a whole)
  • & process in background
  • $? the last command excuted status
  • $$ current process
  • $! the last process in background

command read

  • -t wait for seconds
  • -p print string
  • -s hidden input
  • -n receive n input char and auto enter
#!/bin/bash
read -t 3 -p "Please enter your name: " name
echo "Your name is: $name"
read -s -t 5 -p "Please enter your age: " age
echo -e "\n"
echo "You age is: $age"
read -n 1 -t 30 -p "Please select your gender(m/f): " gender
echo -e "\n"
echo "Your gender is $gender"

Calculating in shell

$ declare -i a=1
  • -i set variable to integer
  • -x set variable to environment variable
  • -p specify type of variable
$ b=$(expr $a + $b)

$((calculate)) or $[calculate]

expressionyy=nully=newValue
x=${y-newVale}x=newValex=nullx=$y
x=${y:-newVale}x=newValex=newValex=$y
x=${y+newVale}x=nullx=newValex=newVale
x=${y:+newVale}x=nullx=nullx=newVale
x=${y=newVale}x=y=newValex=null,yx=$y,y
x=${y:=newVale}x=y=newValex=y=newValex=$y,y
x=${y?newVale}newValue>>stderrx=nullx=$y
x=${y:?newVale}newValue>>stderrnewValue>>stderrx=$y

cut, printf, awk, sed

$ cut -f 1,10 -d ":" /etc/passwd        # line 1-10, separator ":"
$ printf '%s %s %s\n' 1 2 3 4 5 6        # print  1 2 3\n
                                        #         4 5 6\n
                                        
$ sed -n '2p' student.txt                # only print the second line of txt file
$ sed -i '2-4d' student.txt                # delete 2-4 line, change file content with `-i` 
$ sed '2a hello' student.txt            # append "hello" in line 2
$ sed '2i hello \
> world' student.txt                    # insert two lines before line 2 of the file
$ sed '2c No such person' student.txt    # replace line 2 with "No such person"
$ sed -i '3s/99/55/g' student.txt        # replace line 3 string '99' to '55' and both                                             # change the file 
$ sed -e 's/Liming//g ; s/Gao//g' student.txt
                                        # `-e` allow multiple option

sort, wc

$ sort /etc/passwd                        # sort the file by alphabet
$ sort -r /etc/passwd                    # `-r` reverse sort
$ sort -n -t ":" -k 3,3 /etc/passwd        # `-n` handle value with numbers
                                        # `-k n[,m]` from n (to m)
                                        # `-t` sort with seperator ":"
                                        # `-f` ignore capital lowercase
                                        
$ wc /etc/passwd                        # `-l` only line count
                                        # `-w` only word count
                                        # `-m` only char count

test

$ test -e student.txt
$ [ -e student.txt ] && echo yes || echo no
$ [ -z $name="weslie" -a "$a" -gt 3 ] && echo yes || echo no

if

if [condition]; then
    # do something
fi

if [condition]
    then
        # do something
fi

if [condition1]
    then
        # do something
elif [condition2]
    then
        # do something
...
else 
    # do something
fi

case

case $variable in 
    "value1")
        # do something
        ;;
    "value2")
        # do something
        ;;
    ...
    *)
        # do something
        ;;
esac

for

for variable in value1 value2 value3...
    do 
        # do something 
    done

for ((i=0;i<10;i=i+1))
    do 
        # do something
    done

for i in $(file)
    do 
        # do something
    done

while

while [condition]
    do 
        # do something
    done
Last Modified: September 20, 2019
Archives Tip
QR Code for this page
Tipping QR Code