编辑代码

#JSRUN引擎2.0,支持多达30种语言在线运行,全仿真在线交互输入输出。
#bash --version
<<EOF
this shell is for test
this shell is for test
EOF

echo -e "\"Hello\nworld\""
#read var1 var2
#echo $var2
echo '$$'
echo "\$\$"
echo `date`
echo $(date)
echo "the script name is `basename $0`"
echo "var nums:$#"
echo "all user vars:$*"
echo "all user vars:$@"

#characters
text="123456"
length=${#text}
echo ${text:2:($length-3)}
echo `expr index ${text} 34`

#array
color=(red yellow blue)
echo ${color[1]}
echo "${color[*]}"
echo "${color[@]}"
echo "${#color[*]}"
printf "+ %s\n" "${color[@]}"
echo ${#color[@]}
color=(white ${color[@]} black)
unset color[1]
echo ${color[@]}

#condition
x=10
y=20
if [ ${x} != ${y} ]
then
echo  `expr $x \* $y`
elif [ ${x} -ge 10 ]
then
echo "x is greater than 10"
else 
echo "others"
fi

if [ ${x} -gt 1  -o ${y} -gt 1 ]
then echo "x,y are greater than 1"
fi

text2="12"
if [ -n "${text2}" ]
then echo "length of ${text2} is not 0"
fi

if [ -z "${text2}" ]
then echo "length of ${text2} is  0"
fi

if [ ${text2} ]
then echo "${text2} is not empty"
fi

#case
text3="a"
case ${text3} in
"a")
echo "a";;
"b")
echo "b";;
esac

#move file
function moveFile {
    sourceDir="$1"
    targetDir="$2"
    for FILE in ${sourceDir}/*.txt; do
    mv "$FILE" "${targetDir}"
    done
}

sourceDir="`pwd`"
targetDir="`pwd`/text"
touch newFile.txt
chmod 777 newFile.txt
#mkdir -m 777 ${targetDir}
if [ -d ${targetDir} ]
then
echo "dir ${targetDir} exists"
moveFile ${sourceDir} ${targetDir}
else
echo "dir ${targetDir} not exists, create a dir"
mkdir -m 777 ${targetDir}
moveFile ${sourceDir} ${targetDir}
fi

ls -l ${targetDir}


#for while util
for ((i = 0; i < 3; i++)); do
echo "i=${i}"
done

j=0
j=$((j+1))
j=`expr $j + 1` 

while [[ $j < 5 ]]; do
j=$((j + 1))
echo "j=${j}"
done

x=0
#debug
set -x
until [[ $x -ge 3 ]]; do
x=$((x + 1))
echo "x=${x}"
done
set +x


#redirect
    cat <<-EOF >>newFile2.txt
        hello
        hi
EOF
cat newFile2.txt