C shell和TC shell效仿了C语言的预防,儿Bourne shell基于一门古老的编程语言Algol
Bash和Korn shell则综合了Bourne和C shell
Bash Shell语法和结构:
The shbang line
[code]
#!/bin/bash
[/code]
Comment
[code]
# This is a comment
[/code]
Wildcards
[code]
rm *; ls ??; cat file[1-3];
echo "How are you?"
[/code]
Display output
[code]
echo "How are you?"
[/code]
Local variables
[code]
variable_name=value
declare variable_name=value
name="John Doe"
x=5
[/code]
Global variables
[code]
export VARIABLE_NAME=value
declare -x VARIABLE_NAME=value
export PATH=/bin:/usr/bin:.
[/code]
Extracting values from variables
[code]
echo $variable_name
echo $name
echo $PATH
[/code]
Reading user input
[code]
echo "What is your name?"
read name
read name1 name2 ...
[/code]
Arguments
[code]
$ scriptname arg1 arg2 arg3 ...
echo $1 $2 $3
echo $*
echo $#
[/code]
Arrays
[code]
set apples pears peaches (positional parameters)
echo $1 $2 $3
declare -a array_name=(word1 word2 word3)
declare -a fruit=( apples pears plums)
echo $(fruit[0])
[/code]
Command substitution
[code]
variable_name=`command`
variable_name=$( command )
echo $variable_name
echo "Today is `date`"
echo "Today is $(date)"
[/code]
Arithmetic
[code]
declare -i variable_name
typeset -i variable_name
(( n=5 + 5))
echo $n
[/code]
Operators
[code]
==
!=
&&
||
!
>
>=
<
<=
[/code]
Conditional statements
[code]
if command
then
block of statements
else if command
then
block of statements
else
block of statements
fi
case variable_name in
pattern1)
statements
;;
pattern2)
statements
;;
esac
[/code]
Loops
[code]
while command
do
block of statements
done
for variable in word_list
do
block of statements
done
[/code]
Functions
[code]
function_name() {
block of code
}
function function_name {
block of code
}
[/code]
Invitation example of Bash:
[code]
#!/bin/bash
# GNU bash versions 2.x
# The Party Program––Invitations to friends from the "guest" file
guestfile=~/shell/guests
if [[ ! –e "$guestfile" ]]
then
printf "${guestfile##*/} non–existent"
exit 1
fi
export PLACE="Sarotini's"
(( Time=$(date +%H) + 1 ))
declare -a foods=(cheese crackers shrimp drinks `"hot dogs"` sandwiches)
declare -i n=0
for person in $(cat $guestfile)
do
if [[ $person == root ]]
then
continue
else
# Start of here document
mail –v –s "Party" $person <<- FINIS
Hi $person! Please join me at $PLACE for a party!
Meet me at $Time o'clock.
I'll bring the ice cream. Would you please bring
${foods[$n] and anything else you would like to eat?
Let me know if you can make it.
Hope to see you soon.
Your pal,
ellie@$(hostname)
FINIS
n=n+1
if (( ${#foods[*]} == $n ))
then
declare -a foods=(cheese crackers shrimp drinks `"hot dogs"` sandwiches)
n=0
fi
fi
done
printf "Bye..."
[/code]
Bash和Korn shell则综合了Bourne和C shell
Bash Shell语法和结构:
The shbang line
[code]
#!/bin/bash
[/code]
Comment
[code]
# This is a comment
[/code]
Wildcards
[code]
rm *; ls ??; cat file[1-3];
echo "How are you?"
[/code]
Display output
[code]
echo "How are you?"
[/code]
Local variables
[code]
variable_name=value
declare variable_name=value
name="John Doe"
x=5
[/code]
Global variables
[code]
export VARIABLE_NAME=value
declare -x VARIABLE_NAME=value
export PATH=/bin:/usr/bin:.
[/code]
Extracting values from variables
[code]
echo $variable_name
echo $name
echo $PATH
[/code]
Reading user input
[code]
echo "What is your name?"
read name
read name1 name2 ...
[/code]
Arguments
[code]
$ scriptname arg1 arg2 arg3 ...
echo $1 $2 $3
echo $*
echo $#
[/code]
Arrays
[code]
set apples pears peaches (positional parameters)
echo $1 $2 $3
declare -a array_name=(word1 word2 word3)
declare -a fruit=( apples pears plums)
echo $(fruit[0])
[/code]
Command substitution
[code]
variable_name=`command`
variable_name=$( command )
echo $variable_name
echo "Today is `date`"
echo "Today is $(date)"
[/code]
Arithmetic
[code]
declare -i variable_name
typeset -i variable_name
(( n=5 + 5))
echo $n
[/code]
Operators
[code]
==
!=
&&
||
!
>
>=
<
<=
[/code]
Conditional statements
[code]
if command
then
block of statements
else if command
then
block of statements
else
block of statements
fi
case variable_name in
pattern1)
statements
;;
pattern2)
statements
;;
esac
[/code]
Loops
[code]
while command
do
block of statements
done
for variable in word_list
do
block of statements
done
[/code]
Functions
[code]
function_name() {
block of code
}
function function_name {
block of code
}
[/code]
Invitation example of Bash:
[code]
#!/bin/bash
# GNU bash versions 2.x
# The Party Program––Invitations to friends from the "guest" file
guestfile=~/shell/guests
if [[ ! –e "$guestfile" ]]
then
printf "${guestfile##*/} non–existent"
exit 1
fi
export PLACE="Sarotini's"
(( Time=$(date +%H) + 1 ))
declare -a foods=(cheese crackers shrimp drinks `"hot dogs"` sandwiches)
declare -i n=0
for person in $(cat $guestfile)
do
if [[ $person == root ]]
then
continue
else
# Start of here document
mail –v –s "Party" $person <<- FINIS
Hi $person! Please join me at $PLACE for a party!
Meet me at $Time o'clock.
I'll bring the ice cream. Would you please bring
${foods[$n] and anything else you would like to eat?
Let me know if you can make it.
Hope to see you soon.
Your pal,
ellie@$(hostname)
FINIS
n=n+1
if (( ${#foods[*]} == $n ))
then
declare -a foods=(cheese crackers shrimp drinks `"hot dogs"` sandwiches)
n=0
fi
fi
done
printf "Bye..."
[/code]