100% found this document useful (1 vote)
399 views

Shell

This document summarizes key elements of shell scripting including variables, conditionals, loops, functions, commands, and I/O redirection. It provides examples of how to define and use variables, conditionals like if/else and case statements, loops like for and while, functions, commands for testing files and strings, and I/O redirection operators. It also includes a short example shell script that uses some of these elements.

Uploaded by

plperez
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
399 views

Shell

This document summarizes key elements of shell scripting including variables, conditionals, loops, functions, commands, and I/O redirection. It provides examples of how to define and use variables, conditionals like if/else and case statements, loops like for and while, functions, commands for testing files and strings, and I/O redirection operators. It also includes a short example shell script that uses some of these elements.

Uploaded by

plperez
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

SHELL

#!/bin/sh # path to shell interpreter

#variables
$# # number of arguments given
$0 # script name
$1, $2, … # first, second, … arguments
$* # list of all arguments
$? # return value of the last command

$Myvar # value of a variable named MyVar


MyVar=value # assignment of value to MyVar, no spaces beside =

#structures
if command; then # command would be in general the test
instructions1 # command (see below)
elif command; then
instructions2
else
instructions3
fi

case $var in
pattern1) # pattern can be i.e.: yes|Yes|YES
instructions1 # or [Nn][Oo],…
;;
pattern2)
instructions2
;;
*)
instructions3
;;
esac

for newvar in list; do # newvar takes each value in list


instructions
done

while command; do # i.e.: while [ $# -ge 1 ]; do


instructions # echo $1; shift
done # done

funcname () { # create a function whose name is funcname


instructions # warning: $1 would be first argument
}

#useful commands
read myvar # puts stdin until \n in myvar
echo “chaine” # prints chaine to stdout (for errors use echo “chaine”
# 1>2&
cat file # echo file
`command` # value echoed by command ( ` is a backquote)
[ -args ] # you can use test –args instead
# -n chaine la chaine n’est pas vide
# -z chaine la chaine est vide
# chaine1 = chaine2 les 2 chaines sont égales
# chaine1 != chaine2 les 2 chaines sont différentes

# int1 -eq int2 égalité


# -ne différents
# -ge >=
# -gt >
# -le <=
# -lt <

# -e name name exite


# -d répertoire
# -f fichier
# -h lien symbolique
# -r peut être lu
# -w peut être écrit
# -x peut être exécuté

#! not
# -a and
# -o or

# par exemple : [ -r $file –a ! -d $file ] est-ce que file est lisible et n’est pas un répertoire ?
# man test

#redirections
1>&2 # stdout to stderr
2> file # stderr to file
> file # stdout, stderr to file (overwrites)
>> file # stdout, stderr to file (appends)
< file # file to stdin

# mathematics
echo '2 + 4' | bc –l # compute 6 using bc

Exemple de programme

#!/bin/sh # interpreter

# prints all files in $*

getrid () { # function getrid


for file in $*; do
if [ -f $file ]; then
echo ‘’$file’’
fi
done
return 0 # return 0 if ok
}

availFile=`getrid $*`
echo ‘’$availFile’’

You might also like