2008-01-25
学习shell咯3
C shell和TC shell效仿了C语言的预防,儿Bourne shell基于一门古老的编程语言Algol
Bash和Korn shell则综合了Bourne和C shell
Bash Shell语法和结构:
The shbang line
Comment
Wildcards
Display output
Local variables
Global variables
Extracting values from variables
Reading user input
Arguments
Arrays
Command substitution
Arithmetic
Operators
Conditional statements
Loops
Functions
Invitation example of Bash:
} == $n ))
then
declare -a foods=(cheese crackers shrimp drinks `"hot dogs"` sandwiches)
n=0
fi
fi
done
printf "Bye..."
Bash和Korn shell则综合了Bourne和C shell
Bash Shell语法和结构:
The shbang line
#!/bin/bash
Comment
# This is a comment
Wildcards
rm *; ls ??; cat file[1-3]; echo "How are you?"
Display output
echo "How are you?"
Local variables
variable_name=value declare variable_name=value name="John Doe" x=5
Global variables
export VARIABLE_NAME=value declare -x VARIABLE_NAME=value export PATH=/bin:/usr/bin:.
Extracting values from variables
echo $variable_name echo $name echo $PATH
Reading user input
echo "What is your name?" read name read name1 name2 ...
Arguments
$ scriptname arg1 arg2 arg3 ... echo $1 $2 $3 echo $* echo $#
Arrays
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])
Command substitution
variable_name=`command` variable_name=$( command ) echo $variable_name echo "Today is `date`" echo "Today is $(date)"
Arithmetic
declare -i variable_name typeset -i variable_name (( n=5 + 5)) echo $n
Operators
== != && || ! > >= < <=
Conditional statements
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
Loops
while command do block of statements done for variable in word_list do block of statements done
Functions
function_name() {
block of code
}
function function_name {
block of code
}
Invitation example of Bash:
#!/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发表评论
- 浏览: 723111 次
- 性别:

- 来自: BJ

- 详细资料
搜索本博客
我的相册
screenshot
共 1 张
共 1 张
最近加入圈子
最新评论
-
Why OO sucks
看成去QQ SUCK 了
-- by xhanxhanxhan -
Rails的富文本编辑器插件 ...
有没有那个像textmate那种语法高亮的富文本编辑器?
-- by zllicho -
翻译www.djangobook.com之 ...
weiertzw 写道 1. >>> from django. ...
-- by chenjihua75 -
PHP、CakePHP哪凉快哪呆 ...
这孩子被java毒害太深。。。跳出java,你会发现外面的世界真的很大。
-- by woodless -
学习svn命令
只会用 apt-get 不是好孩子。
-- by smartly






评论排行榜