shell脚本

来源:互联网 发布:java管理系统有哪些 编辑:程序博客网 时间:2024/06/08 07:22


1  数组的使用

#!/bin/bash

name="zhangdiqing"

echo ${#name}
echo ${name:1:4}

echo $name $name


name="zhangdiqing"
echo `expr index "$string" is`


array_name=(zhang,wang,li,zhao)
array=(
zhang
wang
li
zhao)


#the index no bondary
array_name[0]=qing


#read the array
echo ${array_name[0]}
#read all
echo ${array_name[@]}


#gain the len
len=${#array_name[@]}
len=${#array_name[*]}


2. 运算符的使用

#!/bin/bash


val=`expr 2 + 2`


echo $val


a=10
b=12
if [ $a -eq $b ]
 then
  echo "a==b"
fi


if [ $a -ne $b ]
 then
 echo "a!=b"
fi


if[ $a -gt $b ]  
# a>b
 then
 echo "a>b"
fi


if  [ $a -lt $b ] 
#a<b
then
echo "a<b"
fi


if [ $a -ge $b ] 
# a>=b
then
echo "a>=b"
fi


if [ $a -le $b ]  #a<=b
then
echo "a<=b"
fi
 
if [ ! false ]
 then
 echo "!false"
fi


if [ false -o true ] # bool or
 echo "false -o true"
fi




# [ false -a true ] #bool  and
#
#
#
#  [0&&7]  #logical and
#  [0||1]  #logical or


3. 参数的使用


#!/bin/bash
echo "$0"
echo "$1"
echo "$2"
echo "$3"

#argu numbers
echo $#  

#the current pid
echo $$

#后台运行的最后一个进程的PID
echo $!

#最后一条命令的执行结果,0表示没有错误
echo  $?

echo $*
echo $@

#显示shell使用的当前选项
echo $-

4.函数

[ function ] funname [()]{    action;    [return int;]}
文件包含:
source filename