欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

linux shell 编程基础总结

程序员文章站 2022-06-24 17:46:59
...

下面是关于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 [email protected]

for param in $*; do
  echo $param
done
unset param
for param in [email protected]; do
  echo $param
done

for param in "$*"; do
 echo $param
done
for param in "[email protected]"; 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'




 

相关标签: bash/shell linux