Shell基础语法测试脚本

本文深入探讨了Shell脚本的优势及其在Linux环境中的使用,包括变量、数组、条件判断、循环、文件操作等核心概念。通过示例展示了如何编写和执行Shell脚本,以及如何进行算术运算、字符串处理和流程控制,为日常Linux系统管理和自动化任务提供实用技巧。
摘要由CSDN通过智能技术生成

#!/bin/bash

###shell脚本的优势是什么,其使用的是什么,基于Linux强大的可用指令?


#read  -s  -p "请输入您的密码:" pass
echo "您输入的密码是 $pass"

your_name="runoob.com.cn"
myname="wulijiao"
readonly myname
unset myname
test_array=(1 2 3 4 5 6 7 8 9 0)
test_array=(
 1
 2
 3
 4
 5
 6
 7
 8
 9
 0
 )
 
 test_array[0]=abc
 str="www.runoob.com/linux/linux-shell-variable.html"
 
echo "$your_name"
echo $your_name
echo ${your_name}
echo "hello,I know your name is \"${your_name}\""
echo "${#your_name}" #get the length of string
echo "str%%*/    : ${str%%*/}"
echo "00 ${your_name:1:3}" #get the one to three chars
echo "01 test_array 0 = ${test_array[0]}"
echo "02 ${test_array[@]}"
echo "03 ${#test_array[*]}"
echo "04 ${#test_array[0]}"
echo "05 ${your_name#*.}" #delete the chars all on the left begin "" #-left the first char
echo "06 ${your_name##*.}" #delete the chars all on the left begin the rightest ##-left and the latest char
echo "07 ${your_name%.*}" #?the first few "."
echo "08 ${your_name%%*.}"
echo "09 ${your_name%%.*}" #right and the first char
echo "10 ${your_name:5}" #from 5th char untill end
echo "11 ${your_name:0-9:2}" #from right untill 9th  2 chars
echo "12 ${your_name:0-9}" #from right untill finish
echo "13 .the number of parameters: $#" #the number of parameters
echo "14 .the first parameter: $1"
echo "15 .print all parameters of input: $*"
echo "15 .the id of current process: $$"
echo "16 .the laster processs' id in backstage:$!"
expr length "$your_name"

#Transfer parameters
#arithmetic operator
value=`expr 2 + 2`
echo "the summ of 2+2 = $value"
value=`expr 3 - 1`
echo "the substraction of 3-1 = $value"
value=`expr 2 \* 3`
echo "the multiplication of 2*3 = $value"
value=`expr 6 / 2`
echo "the division of 6/2=$value"
value=`expr 11 % 10`
echo "the remainder of 11%10=$value"
##judgement
nvala=10
nvalb=20
if [ $nvala == $nvalb ]
then 
    echo "nvala == nvalb"
fi

if [ $nvala != $nvalb ]
then 
    echo "nvala != nvalb"
fi

##relation arithmetic operator
a=10
b=20
if [ $a -eq $b ]
then 
    echo "$a -eq $b equal"
else
    echo "$a -eq $b:not equal"
fi 

if [ $a -ne $b ]
then
    echo "$a -ne $b not equal"
else
    echo "$a -ne $b not equal not"
fi

if [ $a -gt $b ]
then 
    echo "$a -gt $b greater more"
else
    echo "$a -gt $b not greater moree"
fi

if [ $a -lt $b ]
then 
    echo "$a -lt $b less more"
else
    echo "$a -lt $b not less more"
fi

if [ $a -ge $b ]
then 
    echo "$a -ge $b greater equal"
else
    echo "$a -ge $b not greater equal"
fi

if [ $a -le $b ]
then 
    echo "$a -le $b less equal"
else
    echo "$a -le $b not less equal"
fi

##bool arithmetic operator 
if [ $a != $b ]
then 
    echo "$a != $b not equal"
else
    echo "$a != $b not less equal"
fi

if [ $a -lt 20 -o $b -ge 20 ]
then 
    echo "$a -lt 20 -o $b -ge 20"
else
    echo "$a -lt 20 -o $b -ge 20 not"
fi

if [ $a -lt 20 -a $b -ge 20 ]
then 
    echo "$a -lt 20 -a $b -ge 20"
else
    echo "$a -lt 20 -a $b -ge 20 not"
fi

###logical arithmetic operator
if [[ $a -lt 20 && $b -ge 20 ]]
then 
    echo "$a -lt 20 && $b -ge 20"
else
    echo "$a -lt 20 && $b -ge 20 not"
fi

if [[ $a -lt 20||$b -ge 20 ]]
then 
    echo "$a -lt 20 || $b -ge 20"
else
    echo "$a -lt 20 || $b -ge 20 not"
fi

###character string arithmetic operator
stringa="wulijiao"
stringb="user"
if [ $stringa = $stringb ]
then 
    echo "$stringa = $stringb"
else
    echo "$stringa = $stringb not"
fi

if [ $stringa != $stringb ]
then 
    echo "$stringa != $stringb"
else
    echo "$stringa!== $stringb not"
fi

if [ -z $stringb ]
then 
    echo "-z $stringb check the length of string is 0"
else
    echo "-z $stringb check the length of string isn't 0"
fi

if [ -n $stringb ]
then 
    echo "-n $stringb check the length of string isn't 0"
else
    echo "-n $stringb check the length of string is 0"
fi

if [ $stringb ]
then 
    echo "$stringb the string is null"
else
    echo " $stringb the string isn't null"
fi

###file test arithmetic operator
file="/sdc/wulijiao910129/test.sh"
if [ -r $file ]
then 
    echo "-r the $file is can be read"
else
    echo "-r the $file isn't can be read"
fi

if [ -w $file ]
then
    echo "-w the $file can be write"
else 
    echo "-w the file isn't can be write"
fi

if [ -s $file ]
then
    echo "-s the $file is null"
else
    echo "-s the $file isn't null"
fi

if [ -e $file ]
then 
    echo "-e the $file is exit"
else
    echo "-e the $file isn't exit"
fi

if [ -f $file ]
then 
    echo "-f the $file is ordinary file"
else
    echo "-f the $file isn't ordinary file"
fi

###printf and the changer/escape character
printf "%-10s %-8s %-4s\n"       name gender weight
printf "%-10s %-8s %-4s\n"  郭靖 男 66.1234 

printf %s abcdefg "\n"
printf %s adc def 
printf "%s and %d \n"

###test 
##value test -eq -nq -gt -lt -ge -le
if test $[a] -eq $[b]
then 
    echo "the values is equal"
else
    echo "the values isn't equal"
fi

##string test = != -z -n
if test $stringa = $stringb
then 
    echo "the strings is equal"
else
    echo "the strings isn't equal"
fi

##file test -e -r -w -s -f -x -b 
if test -e $file
then 
    echo "test -e the $file is exit"
else
    echo "test -e the file isn't exit"
fi

if test -e $file -o -e ./test.sh
then 
    echo "test -e -o at least one file exit"
else
    echo "test -e -o no file exit"
fi


###
echo "############## process controcol ###############"
if [ $a -eq $b ]
then 
    echo "process $a equal $b"
elif [ $a -lt $b ]
then
    echo "process $a less than $b"
fi

for var in 1 2 3 4 5 6
do 
    echo "for value=$value"
done

for loop in 1 2 3 4 5 6
do 
    echo "loop value $loop "
done

for str in "this is a string"
do 
    echo "str print "$str
done

int=1
while (( $int<=5 ))
do
    echo $int
    let "int++"
    echo "int value = $int"
done

###read something input loop
<<!
while read FILM
do 
    echo "this is the content what you input $FILM"
done
!

###infinite cycle 
<<!
while ture
do 
    echo "cycle ture"
done 

while :
do 
    echo "cycle :"
done 

for (( ; ; ))

untill  [ -z stringa ] #condition
do 
    echo "the stringa is null"
done 
!
value=2
while :
do
case $value in
1)
    echo "case 1 continue"
    continue;
    ;;
2)
    echo "case 2 break"
    break;
    ;;
*)
    echo "this is default value"
    break;
    ;;
esac
done

######founction 
echo "############# founction #########"
demotest(){
    echo "this is a test founction"
}

demotestWithReturn(){
    echo "this founction has return"
    echo "input the first value:"
    read nValueone
    echo "input the second value:"
    read nValuetwo
    return $(($nValueone+$nValuetwo));
}
demotestmain(){
    echo "demotest"
    demotest
    demotestWithReturn
    echo "the return value is:$?"
}

demotestmain


    
:<<!
notes method 1
!

:<<EOF
notes method 2
EOF

exit 0
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值