Bash学习

#!/bin/sh

file=/etc/passwd

if [ -e $file ]
then
  echo "Password file exists."
fi

## if can test any command, no just []
if cmp '$0' logfile &>  /dev/null
then
  echo "Files are identical."
else
  echo "Files are different."
fi

## very useful "if-grep" struct
if grep -q bash $file # "-q" option is used to forbidden ouput
then
  echo "$file contains bash."
fi

word=linux
letter=inu
if echo "$word" | grep -q "$letter"
then
  echo "$letter is found in $word."
else
  echo "$letteer is not found in $word."
fi

echo
echo -n "Test \"0\": "
if [ 0 ]
then
  echo "0 is ture."
else
  echo "0 is flase."
fi
 
echo -n "Test \"1\": "
if [ 1 ]
then
  echo "1 is ture."
else
  echo "1 is flase."
fi

## if and then in the same line, you must write like "if [ -x '$filename']; then".
echo
echo "These commanda are same: test, /usr/bin/test, [], /usr/bin/[."

echo "example: test"
if test -z "$1"; then
  echo "No command-line arguments."
else
  echo "First command-line argument is $1."
fi

echo
echo "example: /usr/bin/test"
if /usr/bin/test -z "$1"; then
  echo "No command-line arguments."
else
  echo "First command-line argument is $1."
fi

echo 
echo "example: []"
if [ -z "$1" ]; then
  echo "No command-line arguments."
else
  echo "First command-line argument is $1."
fi

echo 
echo "example: /usr/bin/["
if /usr/bin/[ -z "$1" ]; then
  echo "No command-line arguments."
else
  echo "First command-line argument is $1."
fi

## not use test/[] after if
dir=/home/elqstux
if cd "$dir" 2>/dev/null
then
  echo "Now in $dir."
else
  echo "Can't change to $dir."
fi
  
## just use test or [], not use if
var1=20
var2=22
[ "$var1" -ne "$var2" ] && echo "$var1 is not equal to $var2."

[ -d "$dir" ] || echo "$dir directory does not exit."


## The (()) cnstruct evaluates and tests numerical expressions.

(( 0 ))
echo "Exit status of \"((0)))\" is $?." #false

(( 5>4 ))
echo "Exit status of \"(( 5>4 ))\" is $?." #ture

### Start find the link file
[ $# -eq 0 ] && directorys=`pwd` || directorys="$@"
echo "The directory is "$directorys""

linkchk()
{
  for element in $1/*; do
  [ -h "$element" -a  -e "$element" ] && echo \"$element\"
  [ -d "$element" ] && linkchk $element
  ## -h is used to test link file; -d is used to test directory
  done
}

for directory in "$directorys"; do
    if [ -d "$directory" ]
	then linkchk "$directory"
    else
	echo "$directory is not a directory."
	echo "Usage: $0 dir1 dir2 ..."
    fi
done
### End find the file link

## the the string is null
if [ -z $str]
then
    echo "String \"str\" is null."
else
    echo "String \"str\" is not null."
fi

echo "Please input the first number:"
read a
echo "please input the second number:"
read b

let "sum = $a * $b"
echo "$a plus $b is $sum."
gcd()
{
  dividend=$1
  divisor=$2
  stop=1
  until [ $stop -eq 0 ]
  do
    let "stop = $dividend % $divisor"
    dividend=$divisor
    divisor=$stop
  done
  echo $dividend
}

res=$(gcd $a $b)
echo;echo "GCD of $a and $b is "$res""
let "res = sum / res"
echo;echo "LCM of $a and $b is "$res""


## && operator, thes commands are same
## 1) if [ $condition1 ] && [ $condition2 ]
## 2) if [ $conditions -a $condition2 ]
## 3) if [[ $conditon1 && $condition2 ]]
## || can't user in [...]

a=24
b=47
if [[ $a -eq 24 && $b -eq 47 ]]
then
    echo "Test #1 succeed!"
else
    echo "Test #1 failed!"
fi

if [ $a -eq 98 ] || [ $b -eq 47 ]
then
    echo "Test #2 succeed!"
else
    echo"Test #2 failed!"
fi

if [ $a -eq 24 -a  $b -eq 47 ]
then
    echo "Test #2 succeed!"
else
    echo"Test #2 failed!"
fi

## , operator
let "t1 = ((5+3, 7-1, 15-4))"
echo "t1=$t1" # t1=11

let "t2 = ((a=9, 15/3))"
echo "t2=$t2 a=$a" # t2=5, a=9


#!/bin/sh

echo "the usage of declare and typeset"

echo "readonly: declare -r var equal to readonly var"
declare -r i

echo "integer: declare -i"
declare -i num
num=3
echo "the value of \"num\" is $num"

num=there
echo "$num" # think num is integer, so the output is 0

n=6/3
echo "n = $n"

declare -i n
n=6/3
echo "n = $n"

echo "array: declare -a"
declare -a arr

echo "function: declare -f"

echo "declare -f: list all the functions."
declare -f > /dev/null 
echo "declare -f function_name: just list the function named function_name"
declare -f f > /dev/null

echo "declare -x: export the variable"
declare -x var=3

echo "declare can use to define local variable"
f()
{
	declare	FOO=dog
}
g()
{
	f
	echo $FOO #print nothing, FOO is a local varible
}
g



## direct reference
a=letter
letter=c
echo "a = $a"

## indirect reference
eval a=\$$a
echo "Now a = $a"


echo "use \$RANDOM produce a random number"
count=10
declare -i j=1
echo -n "produce $count random number:"
while [[ j -le $count ]]
do
	echo -n "$RANDOM "
	j=j+1 #can this, because variable j is declared integer
done
echo

echo "produce a random number range of [a, b]."
f()
{
	low=$1
	high=$2
	declare result=-1
	while [[ $result -le $low ]]
	do
		result=$RANDOM
		let "result = result % $high"
	done
	echo $result
}
f 1 100


swap()
{
	declare -i a=$1
	declare -i b=$2
	declare -i temp
	if [ "$1" -lt "$2" ];then
		temp=a
		a=b
		b=temp
	fi
	echo $a $b
}
a=2;b=6
echo "before swap:a=$a, b=$b."
swap $a $b
echo "after swap:a=$a, b=$b"




## (( ... )) is same as let
a=$(( 3+5 ))
echo "\$(( 3+5 )) = $a"


a=8
echo "the initial value of a is $a"

(( a++ ))
echo "after (( a++ )), the value of  a is $a"

(( --a ))
echo "after (( --a )), the value of a is $a"


(( t = a<45?36:67))
echo "If a<45, then t=36, orelses t=67"
echo "the value of t is $t"


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值