shell


#!/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






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值