Bash脚本基础操作

Bash脚本基础操作

1.Hello world 脚本输出

#! /bin/bash
echo "hello bash script"

需要修改文件属性

chmod +x helloScript.sh

2.重定向至文件(Redirect to file)

#! /bin/bash
echo "hello bash script" > file.txt

将命令行编程文本编辑器,命令行输入的文字将会保存在文件中
退出方式:Ctrl+D
覆盖文本模式:

#! /bin/bash
cat > file.txt

追加文本模式:

#! /bin/bash
cat >> file.txt

3.注释(comments)

通过井号进行单行注释

#! /bin/bash
# This is a comment
cat >> file.txt

通过冒号和单引号进行多行注释

#! /bin/bash
: '
This is a comment
This is a comment
This is a comment'
cat >> file.txt

注意冒号和单引号之间有一个空格

采用<<输出注释

#! /bin/bash

cat << heredocTEXT
This is hello heredocTEXT text
addmore line
heredocTEXT

4.条件语句(conditional statements)

#! /bin/bash
count=10

if [ $count -eq 9 ]
then
    echo "the condition is true"
else
    echo "the condition is false"
fi

等于:eq
不等于:ne
大于:gt

采用符号,需要将方括号变成圆括号

#! /bin/bash
count=10

if (( $count > 9 ))
then
    echo "the condition is true"
else
    echo "the condition is false"
fi
count=10

if (( $count > 9 ))
then
    echo "the condition is true"
elif (( $count <= 9))
then
    echo "the condition is true"
else
    echo "the condition is false"
fi
#! /bin/bash

age=10

if [ "$age" -gt 18 ] && [ "$age" -lt 40 ]
then
    echo "age is corrcet"
else
    echo "age is not correct"
fi
#! /bin/bash

age=10

if [ "$age" -gt 18 -o "$age" -lt 40 ]
then
    echo "age is corrcet"
else
    echo "age is not correct"
fi

5.循环语句(loops)

while和until的使用

#!/bin/bash
n=1
while [ $n -le 10 ]
do
    echo $n
    let n++    #或者写作n=$(( $n + 1 ))
done
#!/bin/bash
n=1
until [ $n -ge 10 ]
do
    echo $n
    let n++    #或者写作n=$(( $n + 1 ))
done

for循环的使用

#!/bin/bash
#{start..ending..increment}
for i in {0..20}
do
    echo $i
done
#!/bin/bash
#{start..ending..increment}
for (( i=0; i<5; i++ ))
do
    echo $i
done

break和continue的使用

#!/bin/bash
for (( i=0; i<=10; i++ ))
do
    if [ $i -eq 3 ] || [ $i -eq 7 ]
    then
        continue
    fi
    echo $i
done

6.脚本输入(Script Input)

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

在这里插入图片描述

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

在这里插入图片描述

#!/bin/bash
args=("$@")
#echo ${args[0]} ${args[1]} ${args[2]}
echo $@
echo $#

在这里插入图片描述

#! /bin/bash
while read line
do
	echo "$line"
done

从文件读取内容:STDIN

#! /bin/bash
while read line
do
	echo "$line"
done < "${1:-/dev/stdin}"

7.脚本输出(Script Output)

STDOUT
STDERR

其中file1.txt中是STDOUT,file2.txt是STDERR

#! /bin/bash
ls -al 1>file1.txt 2>file2.txt
#! /bin/bash
ls -al >file1.txt
#! /bin/bash
ls -al >file1.txt 2>&1
#! /bin/bash
ls +al >& file.txt

8.管道:从一个脚本向另一个脚本发送(Pipes)

在helloScript.sh中

#! /bin/bash
MESSAGE="Hello linux"
export MESSAGE
./secondScript.sh
#! /bin/bash
echo "The message from hello script is $MESSAGE"

9.字符串处理(String processing)

字符串比较是否相等

#! /bin/bash
echo "enter 1st string"
read st1

echo "enter 2nd string"
read st2

if [ "$st1" == "$st2" ]
then
    echo "match"
else
    echo "don't match"
fi

字符串比较大小

#! /bin/bash
echo "enter 1st string"
read st1

echo "enter 2nd string"
read st2

if [ "$st1" \< "$st2" ]
then
    echo "small"
elif [ "$st1" \> "$st2" ]
then
    echo "great"
else
    echo "equal"
fi

大小写转换

#! /bin/bash
echo "enter 1st string"
read st1

echo "enter 2nd string"
read st2

echo ${st1^}
echo ${st1^^}

10.数字与算数

基本算数

#! /bin/bash
n1=4
n2=20
echo $((n1+n2))
echo $((n1-n2))
echo $((n1*n2))
echo $((n1/n2))
echo $((n1%n2))

采用expr方法进行运算

#! /bin/bash
n1=4
n2=20
echo $(expr $n1 + $n2 )
echo $(expr $n1 - $n2 )
echo $(expr $n1 \* $n2 )
echo $(expr $n1 / $n2 )
echo $(expr $n1 % $n2 )

进制转换

#! /bin/bash
echo "enter Hex number of your choice"
read Hex
echo -n "the decimal value of $Hex is : "
echo "obase=10; ibase=16; $Hex" | bc

11.声明命令(Declare Command)

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值