Linux之shell编写

管理整个计算机硬件的其实是操作系统的核心(kernel),这个核心是需要被保护的,所以一般使用者就只能通过shell来跟核心沟通,以让核心达到我们想要达到的工作。man, chmod, chown, vi, fdisk, mkfs 等等指令都是独立的应用程序,但是可以通过壳程序(就是命令行界面)来操作这些应用程序,让这些应用程序调用核心来运行所需的工作。壳程序的功能只是提供使用者操作系统的一个接口。

第一个流行的 shell 是由 Steven Bourne 发展出来的,为了纪念他就称为 Bourne shell ,或直接简称为sh。Linux 使用的这一种版本就称为“ Bourne Again SHell(简称 bash) ”,这个 Shell 是 Bourne Shell 的增强版本,也是基准于 GNU 的架构下发展出来的。检 查/etc/shells 这个文件有多少可以使用的 shells。bash 是 GNU 计 划中重要的工具软件之一,目前也是 Linux distributions 的标准 shell 。 bash主要相容于sh ,并且依据一些使用者需求而加强的 shell 版本。

在 shell 的环境 下,直接输入man bash可以看说明文档。通过 type 这个指令可以知道每个指令是否为 bash 的内置指令。定义变量:

user@ubuntu:~/myshall$ vim myFirShall.sh
user@ubuntu:~/myshall$ man bash
user@ubuntu:~/myshall$ echo ${myvia}

user@ubuntu:~/myshall$ myvia=hhh
user@ubuntu:~/myshall$ echo ${myvia}
hhh

目前shell 环境中, 有多少默认的环境变量?可以利用两个指令来查阅,分别是 env 与 export。set 除了环境变量之外, 还会将其他在 bash 内的变量通通显示出来。

什么是 shell script (程序化脚本) 呢?就字面上的意义分为两部份。 在“ shell ”部分,那是一个命令行下面让我们与系统沟通的一个 工具接口。那么“ script ”是啥? 字面上的意义, script 是“脚本、剧本”的意思。整句话是说, shell script 是针对 shell 所写的剧本。 其实shell script是利用shell 的功能所写的一个“程序 (program)”,这个程 序是使用纯文本文件,将一些 shell 的语法与指令(含外部指令)写在里面, 搭配正则表达式、管线命令与数据流重导向等功能,以达到我们所想要的处理目的。

编写第一个shell script

user@ubuntu:~/myshall$ vim myFirShell.sh    //在vim编辑器编写shell
/
#!/bin/bash
echo "hello world"

user@ubuntu:~/myshell$ ls 
myFirShall.sh
user@ubuntu:~/myshell$ ls -l
total 4
-rw-rw-r-- 1 user user 31 Dec 31 19:17 myFirShell.sh
user@ubuntu:~/myshell$ chmod 777 myFirShell.sh 
user@ubuntu:~/myshell$ ls -l
total 4
-rwxrwxrwx 1 user user 31 Dec 31 19:17 myFirShell.sh
user@ubuntu:~/myshall$ ./myFirShell.sh
hello world

1. 第一行 #!/bin/bash 在宣告这个 script 使用的 shell 名称: 因为我们使用的是 bash ,所以,必须要以“ #!/bin/bash ”来宣告这个文件内的语法使用 bash 的语法!那么当这个程序被执行时就能够载入 bash 的相关环境配置文件 (一般来说就是 non-login shell 的~/.bashrc), 并且执行 bash 来使下面的指令能够执行。(在很多 状况中,如果没有设置好这一行, 那么该程序很可能会无法执行,因为系统可能无法判 断该程序需要使用什么 shell 来执行啊!)

2. 程序内容的说明: 整个 script 当中,除了第一行的“ #! ”是用来宣告 shell 的之外,其他的 # 都是“注解”用途。也可以用sh myFirShell.sh执行。

简单交互 

#!/bin/bash
echo "your name and age"
read name age
echo "your name is: " $name " and age is: " $age


user@ubuntu:~/myshall$ ./myFirShall.sh
your name and age
hhh 22
your name is:  hhh  and age is:  22

数值计算

#!/bin/bash
echo "input two int num"
read -p "first: " first
read -p "second: " second
total=$(($first+$second))
echo "$first + $second = $total"

user@ubuntu:~/myshell$ ./myFirShell.sh
input two int num
first: 3
second: 2
3 + 2 = 5

逻辑运算符

user@ubuntu:~/myshall$ vim myFirShell.sh
user@ubuntu:~/myshall$ ./myFirShell.sh
input filename
filename: hhh.txt
filename do not exist
user@ubuntu:~/myshell$ ./myFirShell.sh
input filename
filename: myFirShell.sh              
file exist

#!/bin/bash
echo "input filename"
read -p "filename: " filename
test -e $filename && echo "file exist" || echo "filename do not exist"

字符串比较

#!/bin/bash
echo "input two string"
read -p "str1: " str1
read -p "str2: " str2
test $str1 == $str2 && echo "str1==str2" || echo "str1!=str2"

user@ubuntu:~/myshell$ ./myFirShell.sh
input two string
str1: ddd
str2: dd
str1!=str2
user@ubuntu:~/myshell$ ./myFirShell.sh
input two string
str1: hhh
str2: hhh
str1==str2

中括号

#!/bin/bash
echo "input two string"
read -p "str1: " str1
read -p "str2: " str2
[ "$str1" == "$str2" ] && echo "str1==str2" || echo "str1!=str2"

user@ubuntu:~/myshall$ ./myFirShall.sh
input two string
str1: fff
str2: ff
str1!=str2
user@ubuntu:~/myshall$ ./myFirShall.sh
input two string
str1: ggg
str2: ggg
str1==str2

条件判断

/
#!/bin/bash
read -p "input[Y/N]: " str1
if [ "$str1" == "Y" ] || [ "$str1" == "y" ]; then
        echo "your input is Y"
        exit 0
fi

if [ "$str1" == "N" ] || [ "$str1" == "n" ]; then
        echo "your input is N"
        exit 0
fi

user@ubuntu:~/myshall$ ./myFirShall.sh
input[Y/N]: y
your input is Y
user@ubuntu:~/myshall$ ./myFirShall.sh
input[Y/N]: n
your input is N
/
#!/bin/bash
read -p "input[Y/N]: " str1
if [ "$str1" == "Y" ] || [ "$str1" == "y" ]; then
        echo "your input is Y"
        exit 0
else
        echo "your input is $str1"
        exit 0
fi

user@ubuntu:~/myshall$ ./myFirShall.sh
input[Y/N]: y
your input is Y
user@ubuntu:~/myshall$ ./myFirShall.sh
input[Y/N]: fff
your input is fff
///
#!/bin/bash
read -p "input[Y/N]: " str1
if [ "$str1" == "Y" ] || [ "$str1" == "y" ]; then
        echo "your input is Y"
        exit 0

elif [ "$str1" == "N" ] || [ "$str1" == "n" ]; then
        echo "your input is N"
        exit 0
else
        echo "your input is $str1"
        exit 0
fi

user@ubuntu:~/myshall$ ./myFirShall.sh
input[Y/N]: y
your input is Y
user@ubuntu:~/myshall$ ./myFirShall.sh
input[Y/N]: n
your input is N
user@ubuntu:~/myshall$ ./myFirShall.sh
input[Y/N]: gfd
your input is gfd
#!/bin/bash
case $1 in
        "a")
        echo "your input is :a"
        ;;
        "b")
        echo "your input is :b"
        ;;
        *)
        echo "your input is other"
        ;;
esac

user@ubuntu:~/myshall$ ./myFirShall.sh a
your input is :a
user@ubuntu:~/myshall$ ./myFirShall.sh b
your input is :b
user@ubuntu:~/myshall$ ./myFirShall.sh dds
your input is other

函数调用

#!/bin/bash
function help(){
        echo "help function is oncall"
}
function close(){
        echo "close function is oncall"
}
case $1 in
        "-h")
        help
        ;;
        "-c")
        close
        ;;
        *)
        echo "your input is other"
        ;;
esac

user@ubuntu:~/myshall$ ./myFirShall.sh -h
help function is oncall
user@ubuntu:~/myshall$ ./myFirShall.sh -c
close function is oncall
user@ubuntu:~/myshall$ ./myFirShall.sh dcsa
your input is other
#!/bin/bash
print(){
        echo "param one: $1"
        echo "param two: $2"
}
print a b

user@ubuntu:~/myshall$ ./myFirShall.sh
param one: a
param two: b

循环

#!/bin/bash

while [ "$str" != "close" ]
do read -p "your input is: " str
done

echo "while close"

user@ubuntu:~/myshall$ ./myFirShall.sh
your input is: csd
your input is: close
while close
#!/bin/bash

for name in hh hhh hhhh
do echo "name is: $name"
done

user@ubuntu:~/myshall$ ./myFirShall.sh
name is: hh
name is: hhh
name is: hhhh

#!/bin/bash
read -p "input count: " count
total=0

for((i=0; i<=count; i=i+1))
do total=$(($i+$total))
done
echo "1+2+3+...+$count=$total"

user@ubuntu:~/myshall$ ./myFirShall.sh
input count: 5   
1+2+3+...+5=15

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值