学习shell咯3

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]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值