Skip to content

Instantly share code, notes, and snippets.

@khanh96le
Last active October 26, 2018 08:16
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save khanh96le/d09b0be79152c41da9c4bf85cd9edd19 to your computer and use it in GitHub Desktop.
Save khanh96le/d09b0be79152c41da9c4bf85cd9edd19 to your computer and use it in GitHub Desktop.
bash basic

Learn at this tutorial http://www.shellscript.sh/

1.HelloWorld

The first line

#!/bin/sh
echo 'hello world'

what is #!/bin/sh means?

  • It likely declare a environment to run the below shell.

If we change the #!/bin.sh to #!/usr/bin/perl, the script would not run

#!/usr/bin/perl
echo 'hello world'

echo

  • echo hello world, echo 'hello world', echo "hello world" => hello world
  • echo hello world, echo 'hello world => hello word, hello world (echo hello world means print 2 argumets, and sh automatically convension the spaces between them, but 'hello world' is a argument, so everything inside the single quotes would be printed
  • echo * => print all file name in the same directory with this file
  • echo `hello` world

2.Variables

Synstax: VAR=value

#!/bin/sh
MESSAGE='Hello World'
echo $MESSAGE

In Bash, variable can be everything. If you want to calculate, you have to use expr

NUMBER=1
echo $NUMBER + 3
expr $NUMBER + 3

Read value from the keyboard

We use read VAL. The value from keyboard input would be equaled to VAL

$!/bin/sh
echo 'What is your name?'
read NAME
printf ">> " #printf to print in a single line.
echo "Hi $NAME - Hope you're well"

Scope of Variables

  • variable in bracket
#!/bin/sh
echo "What is your name?"
printf ">> "
read USER_NAME
echo "Hello $USER_NAME"
echo "I Will create you a file called $USER_NAME_file" #????
touch $USER_NAME_file

The code above would return error when we echo $USER_NAME_file, because, the shell does not know where is the end of variable. Replace code by

echo "I Will create you a file called ${USER_NAME}_file
touch ${USER_NAME}_file

3. Wildcard

  • Copy all .txt files, or .html files in a folder
cp /tmp/a/*.txt tmp/b
cp /tmp/a/*.html tmp/b
  • Remane all files .txt to .html
mv *.txt *.bak

4. Escape Characters

5. Loop

  • for in array
#!/bin/sh
for i in 1 2 3 4 5 
do
  echo "Looping ... $i"
done

*Note: array value can be anything

  • for in range: In order to use for a range, the first line must declare #!/bin/bash
#!/bin/bash
for i in {1..100}
do
  echo $i
done
  • while
#!/bin/sh
while [ $INPUT_NAME != "bye" ] #Remember always has 2 space in the head and tail of squarebracket
do
  echo "Type anything (bye to quit)"
  read INPUT_NAME
  echo "You type $INPUT_NAME" 
done
  • while true
while :
do 
  echo "type something (Ctrl C to quit)
  read $INPUT_STRING
  echo "You type $INPUT_STRING"
done
  • while read f: Read line by line in a file
#!bin/bash
while read f
do
  echo $f
done < file.txt
while f=`line`
do
  case $f in
    hello)
      echo English
    ;;
    howdy)
      echo American
    ;;
    gday)
      echo Australian
    ;;
    bonjour)
      echo French
    ;;
    "guten tag")
      echo German
    ;;
    *)
      echo Unknown Language: $f
    ;;
  esac
done < language.txt

6. Conditional

if [ EXPR ]; then
  processing...
fi

[Watch all EXPR here] (http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_07_01.html)

7. Advance Variable

How can we run command like this: sudo get-apt update??

All things follow behind sudo are variables. This example shows that. We make a var.sh:

#!/bin/bash
echo "I was called with $# parameters"
echo "My name is $0"
echo "My first parameter is $1"
echo "My second parameter is $2"
echo "My third parameter is $3"
echo "All parameters are $@"

Now run ./var.sh hello world earth

  • $0: basename
  • $#: number of parameters
  • $@: list parameters
  • $1 .. $9: parameters

Run more than 9 parameters, using shift

#!/bin/sh
while [ "$#" -gt "$0" ]
do
  echo "$1 is $1"
  shift
done

IFS variable

#!/bin/sh
old_IFS="$IFS"
IFS=:
echo "Please input three data separated by colons ..."
read x y z
IFS=$old_IFS
echo "x is $x y is $y z is $z"

8. function

Syntax:

function_name() {
  code here..
}
function_name parameter1 paraeter2 

Example:

#!/bin/sh
# Simple script with a function
add_a_user() {
  USER=$1
  PASSWORD=$2
  shift; shift;
  #Having shifted twice, the rest is now comments
  COMMENTS=$@
  echo "Adding user $USER ..."
  echo useradd -c "$COMMENTS" $USER
  echo passwd $USER $PASSWORD
  echo "Added user $USER ($COMMENTS) with pass $PASSWORD"
}

###
#Main body
##
echo "Start of script"
add_a_user khanhle 1409 Le Khanh the presenter
#add_a_user Phobe badpasswd Phobe Caufield the singer
#add_a_user Holden worsepassword Holden Caufiled the student
echo "End of script"

NOTE: Using shift to forget the next parameter

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment