学习shell scripts

变量赋值

使用declare -i 定义整型类

[[email protected] scripts]# aa=5+6
[[email protected] scripts]# echo $aa
5+6
[[email protected] scripts]# declare -i aa
[[email protected] scripts]# aa=5+6
[[email protected] scripts]# echo $aa
11

使用let,就无需定义为整型

[[email protected] scripts]# let bb=1+8
[[email protected] scripts]# echo $bb
9

[[email protected] scripts]# cc=$((5+8))
[[email protected] scripts]# echo $cc
13

echo $? 返回值

test 比较两个值得大小

-eq -lt -le -gt -ge

aa=3 bb=4

test $aa -eq $bb

echo $?

test $aa -lt $bb

echo $?

判断1 || 判断2

[ -f /etc/passwd ] 判断文件是否存在

echo $?

0

[ -r /etc/passwd ]

echo $?

0

[[email protected] scripts]# aa=3 bb=4
[[email protected] scripts]# [ $aa -lt $bb ] && echo ok
ok
[[email protected] scripts]# [ $aa -gt $bb ] && echo ok 
[[email protected] scripts]# [ $aa -gt $bb ] || echo ok  
ok

判断代码

[[email protected] scripts]# vim if.sh
#!/bin/bash
grep -q ^$1 /etc/passwd
if [ "$?" -eq 0 ]; then
        echo "$1 is exist"
else 
        echo "$1 is not exist"
fi

if...then

[[email protected] scripts]# vim age.sh
#!/bin/bash
read -p "Please input your age: " age
if [ "$age" -le 0 ] || [ "$age" -ge 100 ]; then
        echo "Please input correct age"
        read age
elif [ "$age" -gt 0 ] && [ "$age" -lt 20 ]; then
        echo "You are junior"

elif [ "$age" -ge 20 ] && [ "$age" -lt 50 ]; then
        echo "You are adult"
else
        echo "You are old"
fi

while循环

[[email protected] scripts]# vim sum.sh
#!/bin/bash

sum=0
while [ $sum -lt 10 ]
do
        let sum+=1
        echo $sum
done
#!/bin/bash

read -p "Please input your name: " name
while [ "$name" != tom ] 
do
        echo "Please input correct name"
        read name
done
~
[[email protected] scripts]# vim case.sh
#!/bin/bash
xx=0
until [ "$xx" -gt 24 ]
do
        case "$xx" in
                [0-9]|1[01])
                        echo "good morning"
                        ;;
                12)
                        echo "it‘s lunch time"
                        ;;
                1[3-7])
                        echo "good afternoon"
                        ;;
                *)
                        echo "good evening"
        esac 
        let xx+=1
done
时间: 2024-08-09 00:08:17

学习shell scripts的相关文章

第十三章、学习 Shell Scripts

1. 什么是 Shell Script 1.1 干嘛学习 shell scripts 1.2 第一支 script 的撰写与运行 1.3 撰写 shell script 的良好习惯创建 2. 简单的 shell script 练习 2.1 简单范例: 对谈式脚本, 随日期变化, 数值运算 2.2 script 的运行方式差异 (source, sh script, ./script) 3. 善用判断式 3.1 利用 test 命令的测试功能 3.2 利用判断符号 [ ] 3.3 Shell sc

鸟哥的Linux私房菜——第十六章:学习Shell Scripts

视频链接: 1. 什么是 Shell Script       (shell写的脚本)1.1 干嘛学习 shell scripts? ()1.2 第一支 script 的撰写与执行1.3 撰写 shell script 的良好习惯建立 2. 简单的 shell script 练习: (read -p  date)3. 善用判断式:3.1 利用 test 指令的测试功能3.2 利用判断符号 [ ] 3.3 Shell script 的预设变数($0, $1...)4. 条件判断式:4.1 利用 i

鸟哥的Linux私房菜_基础版_学习笔记9:第十三章 学习 Shell Scripts

13.1 什么是 Shell scripts 13.1.1 干嘛学习 shell scripts 13.1.2 第一支 script 的撰写与运行 在 shell script 的撰写中还需要用到底下的注意事项: 命令的运行是从上而下.从左而右的分析与运行: 命令的下达就如同第五章内提到的: 命令.选项与参数间的多个空白都会被忽略掉: 空白行也将被忽略掉,并且 [tab] 按键所推开的空白同样视为空白键: 如果读取到一个 Enter 符号 (CR) ,就尝试开始运行该行 (或该串) 命令: 至於

chapte13:学习shell scripts之(1)简单的shell scripts

简单的说就是将一些shell的语法与命令写在里面,加上正则表达式,管道命令与数据流重定向等功能,以达到我们想要的目的. shell scripts用在系统管理上是一个很好的工具,但是用在处理大量计算时,则速度不够,且使用的CPU资源较多. 一.第一个scripts编写与执行 假设的我们写的第一个scripts文件名为shell.sh,执行的方法有如下几种: (1)直接命令执行:shell.sh文件要有rx(读与执行)的权限. 绝对路径:/home/chuiyuan/shell.sh 相对路径:在

拜师鸟哥之linux学习体会(12)——学习shell scripts

1.    shell script其实就是利用shell的功能所写的一个程序,这个程序是使用纯文本文件,将一些shell的语法与指令写在里面,搭配正规表示法.管线命令|与数据流重导向等功能,以达到我们想要处理的目的. 2.    script最简单的功能就是会整一些在command line下达的连续指令,将他写入scripts中,而由直接执行scripts来启动一连串的command line指令输入,这下明白了吧,就相当于写个脚本程序,里面能够执行多条指令,这样我们就不用每次都要输好多条指

第十三章、学习 Shell Scripts 简单的 shell script 练习

简单的 shell script 练习 简单范例 对谈式脚本:变量内容由使用者决定 [[email protected] scripts]# vi sh02.sh #!/bin/bash # Program: # User inputs his first name and last name. Program shows his full name. # History: # 2005/08/23 VBird First release PATH=/bin:/sbin:/usr/bin:/us

第十三章、学习 Shell Scripts 条件判断式

利用 if .... then 单层.简单条件判断式 if [ 条件判断式 ]; then 当条件判断式成立时,可以进行的命令工作内容: fi <==将 if 反过来写,就成为 fi !结束 if 之意! && 代表 AND : || 代表 or : [ "$yn" == "Y" -o "$yn" == "y" ]上式可替换为 [ "$yn" == "Y" ] ||

第十三章、学习 Shell Scripts 循环 (loop)

while do done, until do done (不定循环) while [ condition ] <==中括号内的状态就是判断式 do <==do 是回圈的开始! 程序段落 done <==done 是回圈的结束 until [ condition ] do 程序段落 done [[email protected] scripts]# vi sh13.sh #!/bin/bash # Program: # Repeat question until user input c

鸟哥私房菜基础篇:学习 Shell Scripts习题

猫宁!!! 参考链接:http://cn.linux.vbird.org/linux_basic/0340bashshell-scripts.php 鸟哥是为中国信息技术发展做出巨大贡献的人. 1-请创建一支 script ,当你运行该 script 的时候,该 script 可以显示: 1. 你目前的身份 (用 whoami ) 2. 你目前所在的目录 (用 pwd) #!/bin/bash echo -e "Your name is ==> $(whoami)" echo -