#!/bin/bash
echo "please input your name:"
read name
echo "your name is $name"
string="hello shell"
echo ${#string} # 11
echo $0 $1 $2 $3 $@ $#
num=6
if (($num > 7)) #if [ $num -gt 7 ]
then
echo "6 is bigger"
elif (($num < 7))
then
echo "7 is bigger"
else
echo "there are equal"
fi
word=bc
if [ $word = "abc" ]
then
echo "string is equal"
elif [[ $word > "abc" ]] #98>97
then
echo "bc less than abc"
fi
echo "please input name of file:"
read filename
if [ -e $filename ] #文件是否存在exist
then
echo "file is found"
else
echo "file is not found"
fi
if [ -f $filename ] #是否爲常規file
then
echo "$filename found"
else
echo "$filename is not found"
fi
if [ -d $filename ] #是否爲目錄
then
echo "$filename found"
else
echo "$filename is not found"
fi
if [-s $filename ] #文件存在且不爲空
then
echo "$filename is not empty"
else
echo "$filename is empty"
fi
向文件末尾追加內容
#!/bin/bash
echo "please input filename"
read filename
if [ -f $filename ]
then
if [ -w $filename ]
then
echo "please input content, press ctrl+d exit."
cat >> $filename
else
echo "file is readonly"
fi
else
echo "file is not exist."
fi
邏輯與,邏輯或 && || -a -o
#!/bin/bash
x=10
y=20
if(($x < $y)) && (($x > 5))
then
echo "&& condition is true"
fi
if(($x > $y)) || (($y > 10))
then
echo "|| condition is true"
fi
if [ $x -lt $y -a $x -lt 20 ]
then
echo "-a condition is true"
fi
if [ $x -gt $y -o $y -lt 30 ]
then
echo "-o condition is true"
fi
算數運算
#!/bin/bash
x=10
y=20
echo $((x + y))
echo $((x - y))
echo $((x * y))
echo $((x / y))
echo $((x % y))
case....in....esac
#!/bin/bash
echo "please input value:"
read value
case $value in
[A-Z])
echo "value is a-z" ;;
[a-z])
echo "value is A-Z" ;;
[0-9])
echo "value is 0-9" ;;
*)
echo "default value" ;;
esac
數組
#!/bin/bash
array=('linux' 'unix' 'windows' 'mac')
array[4]='andriod'
unset array[0]
echo ${array[@]} #unix windows mac andriod
echo ${array[4]} #andriod
echo ${#array[@]} #4
while循環
#!/bin/bash
n=1
while (( $n <= 10))
do
echo $n
(( n++ ))
sleep 1
done
for循環
#!/bin/bash
for i in 1 2 3 4 5
do
echo $i
done
total=0
for ((i=1; i<=100; i++))
do
total=$(( total + i))
done
echo $total