Skip to content

Instantly share code, notes, and snippets.

@scallacs
Last active April 22, 2018 15:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save scallacs/0e8e64bf4653d0c4e18b9905559d530f to your computer and use it in GitHub Desktop.
Save scallacs/0e8e64bf4653d0c4e18b9905559d530f to your computer and use it in GitHub Desktop.

Shell memo

Dev

#!/bin/sh (ou #!/bin/bash) # Header for a shell script

Variables

Declaration

variable=50
variable2=`cat file`

Input

read variable	# read user input

Usage

echo $variable # read a variable

Functions

Declaration

name(){
	cmds...
}

Call

name arg1 arg2 arg3...

Variables related to functions

  • $# : argument number
  • $0 : script file name
  • $$ : PID du processus
  • $i ($1 et +) : argument i
  • $* : all arguments including script name
  • $@ : all arguments without the script name
  • shift n : shift arguments by n
  • $? : exit code of a function

Conditions

Strings

$chaine1 = $chaine2 	# string equals
$chaine1 == $chaine2 	# string equals
$chaine1 != $chaine2 	# string not equals
-z $chaine1 		# empty string
-n $chaine1 		# not empty string

Numbers

$num1 -eq $num2 # =
$num1 -ne $num2	# !=
$num1 -lt $num2 # <
$num1 -le $num2 # <=
$num1 -gt $num2 # >
$num1 -ge $num2 # >=

Files

-a fichier # file exits
-d fichier # is a directory
-f fichier # is a regular file

And / Or / Not

[ condition1 ] && [ condition2 ]
[ condition1 ] || [ condition2 ]
[ ! condition1 ] 

Instructions

If

if [ condition ]
      then
        command1
      elif [ condition2 ]
        command2
      else
        default command
fi

While loop

while [ condition ]
    do
        commands
    done

For loop

for var in `cat files`
     do
      commands
     done

Case

case var in
	value1) instruction ;;
	value2) instruction ;;
	*) default instruction ;;
esac

Usefull commands

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