linux shell 编程基础总结

下面是关于bash编程的基础语法使用总结。

注意区分单引号 ' 和反引号 ` 。

#! /bin/bash

# Two run way:
#   1. bin/sh shellTest.sh
#   2. chmod +x shellTest.sh
#      ./shellTest.sh
 

echo 'My first bash!'
echo "\n ----------------------------Normal var"
var1='var_value1'

# diff between next two lines ' or "
echo 'value of var1 is:${var1}'
echo "value of var1 is:${var1}"
echo "value of var1 is:\"${var1}\""

echo 'Length of var1 is :'${#var1}

echo 'Index of _ in var1 is :'`expr index $var1 _`
echo 'Index of _ in var1 is :'`expr index "${var1}" _`

echo "\n ----------------------------Array var"
arrayVar=(aValue1 aValue2 aValue3 aValue4 aValue5)
arrayVar[0]=aValue1_1
echo "The array length is :${#arrayVar}"
echo "The array length is :${#arrayVar[*]}"
echo "The array values:${arrayVar[@]}"
echo "The array values:${arrayVar[*]}"
for var in "$arrayVar";do
 echo "The array value is:"$var
done

echo "\n ----------------------------Parametor use"
echo "The running file is :$0"
echo "The process id is :$$"
echo "The input param number is :$#"
echo 'The first param is :'$1
echo "The second param is : $2"

echo $*
echo $@

for param in $*; do
  echo $param
done
unset param
for param in $@; do
  echo $param
done

for param in "$*"; do
 echo $param
done
for param in "$@"; do
 echo $param
done

echo "\n ----------------------------Get Input:read"
# -n:max input length; -t:time limit; -s: hide input char
read -p "Please input password" -n 6 -t 5 -s password
echo password is : $password

echo "\n ----------------------------Arithmetic Operator"
echo please input two number:
read num1 num2
echo num1 + num2 = `expr $num1 + $num2`
echo num1 - num2 = `expr $num1 - $num2`
echo num1 \* num2 = `expr $num1 \* $num2`
echo num1 / num2 = `expr $num1 / $num2`
echo num1 % num2 = `expr $num1 % $num2`

# && same as -a  || same as -o
if [ $num1 == $num2 -a $num1 -eq $num2 ]
then
  echo num1== num2
fi
if [ $num1 != $num2 -o $num1 -ne $num2 ]
then
  echo num1 != num2
fi

if [ $num1 -gt $num2 ]
then
  echo num1 \> num2
fi

if [ $num1 -lt $num2 ]
then
  echo num1 \< num2
fi

if [ $num1 -ge $num2 ]
then
  echo num1 \>= num2
fi

if [ $num1 -le $num2 ]
then
  echo num1 \<= num2
fi

echo "\n ----------------------------string test"
read -p "Please input a string:" str
if [ $str ]
then
  echo "input string is: " $str
else
  read -p "Empty input, please try again:" str
fi

if [ -n "$str" ]
then
  echo "Input string length is not zore"
fi
if [ -z $str ]
then
  echo "Input string length is zore"
fi

echo "\n ----------------------------file test"
filepath='/var/run/cron.reboot'
if [ -e $filepath ]
then
  echo "exit file:" $filepath
  if [ -r $filepath ]
  then
    echo "readable file"
  fi
fi

echo "-b, -c, -d, -f, -g, -k, -p, -u, -r, -w, -x, -s, -e"
# -b, -c, -d, -f, -g, -k, -p, -u, -r, -w, -x, -s, -e
echo "\n ----------------------------printf"
printf "%s %c %d %f\n" "adc" "abc" 123 123.1
printf "%10s %10c %10d %10.2f\n" "abc" "abc" 123 123.1  
printf "%-10s %-10c %-10d %-10.2f\n" "abc" "abc" 123 123.123  

echo '----------------------------end'




 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值