shell脚本练习demo

test1.sh

#!/bin/bash

#注释
echo "begin test1.sh"
read -p "请输入num的值:" num
read data1 data2
echo "num=$num"
echo "num1=$1"
echo "num2=$2"
echo "data1=$data1"
echo "data2=$data2"
readonly num=1
echo "num=$num"
num=2
echo "num=$num"
echo "env_data=$LOGNAME"
echo "==================="
echo 'num=$num'
echo "参数的个数=$#"
echo "参数的内容=$*"
echo "第一个参数=$1"
echo "第二个参数=$2"
echo "第三个参数=$3"
echo "进程名=$0"
echo "进程ID=$$"
readonly num=2
num=3
echo "上一个命令的结果=$?"

test2.sh

#!/bin/bash
echo "begin test2.sh"
echo -e  "today is \n  `date`"
ls
num=10
(
    num=100
    echo "()num=$num"
)
echo "num1=$num"
{
    num=200
    echo "{}num=$num"
}
echo "num2=$num"
echo "=========变量的扩展"
#判断变量是否存在
echo ${num11:-100}
num11=200
echo ${num11:-100}

echo ${num22:=50}
echo "num22:=$num22"

str="haha:xixi:hehe"
echo "str的长度为:${#str}"
echo ${str/:/@}
#条件测试
read -p "请输入一个文件名" fileName
result=test -e $fileName
echo "result=$result"
[ -e $fileName ]
echo $?

test -z $sttr
echo $?
read -p "please input y/n:" sttr
[ -z sttr ]
echo "1:$?"
[ $sttr = "y" ]
echo "2:$?"
r

你可能感兴趣的:(bash,batch,linux)