shell 基础

作者: lvyz0207 | 来源:发表于2019-11-03 18:58 被阅读0次

linux:

终止项目进程:
ps -ef | grep rtprecv | grep -v grep | awk '{print $2}' | xargs kill -9

#! /bin/bash
# 新建脚本 并执行 chmod +x ./text.sh



c=20   # 给变量赋值后无空格
d=30
if [ $c == $d ] # 注意[ 与变量之间有空格
then
  echo "ture"
fi
if [ $c != $d ]
then
    echo "c is not equal d"
fi

c=20
d=30
if [ $c == $d ]
then
        echo "c is equal to d"
elif [ $c -gt $d ]           # 大于
then
        echo "c is greater than d"
elif [ $c -lt $d ]  # 小于
then
    echo "c is less than d"
else
    echo "None of the condition met"
fi

echo "===字符串==="
str1="Hello"
str2="World"
str3=$str1' '$str2
echo $str2 # 输出字符串
echo ${#str3}
echo ${str3:1:8}

echo "===数组==="
array=(1 2 3 4 5)
array2=(aa bb cc dd ee)
value=${array[3]}        # 赋值
echo $value
value2=${array2[3]}        # 找到某一个下标数,然后赋值
echo $value2
length=${#array[*]}   # 获取数组长度
echo $length

echo "====echo 使用===="
text="Hello World"
echo $text

echo -e "Hello \nWorld"       # 输出并换行
echo "Hello World" > a.txt   # 重定向到文件

echo `date`    # 输出当前系统时间


echo "===test命令===="
num1=10
num2=11
test $[num1] -eq $[num2]
test 3=5

echo "====for 循环===="
for i in {1..5}
do
    echo $i
done

for FILM in $HOME/.bash
do
    echo $FILM
done

echo "===while循环===="
COUNTER=0
while [ $COUNTER -lt 5 ]
do
    COUNTER=`expr $COUNTER + 1`
    echo $COUNTER
done

# 用户从键盘输入,然后输出用户的内容
echo "请输入....."
echo 'ctrl + d 终止程序'
while read FILM
do
echo "Yeah! great film the $FILM"
done

echo "====函数==="
sysout(){
    echo "hello world"
}

test(){

    aNum=3
    anotherNum=5
    return $(($aNum+anotherNum))
}
test
result=$?
echo $result

echo "====重定向===="
echo result > a.txt # 将结果写入文件,结果不会在控制台输出,而是在文件中 覆盖写
echo result >> a.txt # 追加写

相关文章

网友评论

    本文标题:shell 基础

    本文链接:https://www.haomeiwen.com/subject/vpdlbctx.html