<六>初识shell scripts

一个简单的脚本

脚本可以不会写,但是要会看,会改,当然会写最好啦

#!/bin/bash 
name="Linux"
name1="My name is $name"
name1='My name is $name'
echo $name1
echo $name2

名为test.sh 执行 sh test.sh,第一行声明使用的 shell。(PS:顺便看下单引号和双引号的区别)

运算符declare

  1. shell中定义的变量默认都是字符串形式,如a=3;b=5;c=$a*$5;echo $c 得到的结果为3*5;那么久需要declare声明变量
  2. declare [-afirx] (a:数组;f:函数;i:整数;r:只读;x:通过环境输出变量)
  3. 再来看看一个脚本程序
#!/bin/bash
num1=2*3+5*13-32
declare -i num2=2*3+5*13-32
echo $num1
echo $num2

交互式脚本

简单的说交互式脚本就是程序会根据您输入的数据进行判断。最简单的是read,下面一个简单的例子

#!/bin/bash
echo "input your name"
read name
echo "your name is $name"

现在说说怎么定义一个脚本的参数代号(就是说输入参数有多个,怎么区别第几个)?例:

#!/bin/bash
echo "the scripts is $0"
echo "parameters $1 $2 $3"

运行:sh test.sh pa1 pa2 pa3

脚本逻辑判断式与表达式

在脚本里很重要的一项工作就是判断是否可行,如:当我们要建立以个目录时,得先判断该目录是否存在……
逻辑判断式与if…then…fi的关系密不可分,下面看看

条件判断

  • if…then…fi
    最常用的是if … then … else if … then … end if 语句,下面看一个例子
#!/bin/bash
echo "input y\n"
read yn
if [ "$yn" = "y" ] || [ "$yn" = "Y" ]; then
    echo "script is running"
else 
    echo "STOP!"
fi

需要注意的点:
1. 在[]中,只能有一个判断式
2. 在[] 与[]间,可以用&&或||结合判断式
3. 每一个独立的组件之间需要用空格键隔开([(空格)”$yn”(空格)=(空格])”y”(空格))
再来看下用参数表示

#!/bin/bash
if [ "$1" = "hello" ]; then
    echo "hello Linux"
elif [ "$1" = "" ]; then
    echo "you must input parameters" 
else
    echo "Bye Bye"
fi

现在再看一个长一点的程序(规范点儿的程序),用于检测端口是否开启

!/bin/bash
#program:Using to study the [if ... then ... fi] program
#written by: xxx
#date:2015/10/05
#content:i wiil use this program to show your service
#1. print the program's work in your screen
echo "Now the services of your Linux system will be detect"
echo "The www, sshwill be detect"
echo " "
#2. www
www='netstat -an|grep LISTEN|grep :80'
if [ "$www" != "" ]; then
    echo "WWW is running"
else
    echo "WWW is not runniing"
fi
#3. ssh
ssh='netstat -an|grep LISTEN|grep :22'
if [ "$ssh" != "" ]; then
    echo "SSH is running"
else
    echo "SSH is not runniing"
fi
  • case… esac
    直接看程序
#!/bin/bash
echo "this program will select your selection"
case $1 in
 one)
     echo "your choice is one"
     ;;
 two)
     echo "your choice is one"
     ;;
 three)
     echo "your choice is one"
     ;;
 *)
     echo "Usage {one|two|three}"
     exit 1
esac

换成交互式的,如下:

#!/bin/bash
echo "press your select one two three"
read num
case $num in
 one)
     echo "your choice is one"
     ;;
 two)
     echo "your choice is one"
     ;;
 three)
     echo "your choice is one"
     ;;
 *)
     echo "Usage {one|two|three}"
     exit 1
esac

循环

  • 语句
    • for ((条件1; 条件2; 条件3))
    • for variable in variable1 variable2
    • while [condition1] && { || } [condition2] …
    • until [condition2] && { || } [condition2]…
      来看一个for的程序
#!/bin/bash
declare -i s
for (( i=1; i<=100; i=i+1))
do
    s=s+i;
done
echo "sum = $s"

下面是while 版本

#!/bin/bash
declare -i i
declare -i s
while [ "$i" != "101" ]
do 
    s=s+i;
    i=i+1;
done
echo "sum is $s"

下面是until 版本

#!/bin/bash
declare -i i
declare -i s
until [ "$i" = "101" ]
do 
    s=s+i;
    i=i+1;
done
echo "sum is $s"

下面看下for另一种循环方式用于非数字

#!/bin/bash
LIST="David Lili Tom"
for i in $LIST
do
    echo $i
done

再来看一个例子,输出主机账号

#!/bin/bash
account=`cut -d ":" -f1 /etc/passwd | sort`
for i in $account
do
    echo $i
done

注意上述是反引号,而不是单引号
下面看一个程序判断有没有文件(-e判断)或该文件是目录还是文件(-d,-f),就用当前目录下的test看试验下吧

#!/bin/bash
if [ ! -e test ]; then
    touch test
    echo "just make a file test"
    exit 1
elif [ -e test ] && [ -f test ]; then
    rm test
    mkdir test
    echo "remove file ==> test"
    echo "and make dir test"
    exit 1
elif [-e test ] && [ -d test ]; then
    rm -rf test
    echo "remove dir test"
    exit 1
else
    echo "does here has anything?"
fi

调试脚本

语法:sh [-nvx] scripts
-n : 不执行脚本,查询脚本内的语法,若有错误则列出
-v : 执行脚本之前,先将脚本的内容显示在屏幕上
-x : 将用到的脚本内容显示在屏幕上,与 -v稍微不同

关于

shell脚本程序应该多学习,多看,多模仿,这里仅仅是初学习shell 入门知识

参考 《鸟哥的Linux私房菜》

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
<!DOCTYPE HTML> <html> <head> <title>搜索网站</title> <meta charset="utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no" /> <link rel="stylesheet" href="assets/css/main.css" /> <!--[if lte IE 9]><link rel="stylesheet" href="assets/css/ie9.css" /><![endif]--> <noscript><link rel="stylesheet" href="assets/css/noscript.css" /></noscript> </head> <body> <!-- Wrapper --> <div id="wrapper"> <!-- Header --> <header id="header"> <div class="logo"> <span class="icon fa-diamond"></span> </div> <div class="content"> <div class="inner"> <h1>你好</h1> <p><!--[-->尊敬的用户,欢迎使用本网站<!--]--></p> <p><!--[-->本网站将为将您提供一下服务,如您有更好的意见,欢迎致信我们的邮箱 <!--]--></p> <p><!--[-->[email protected]<!--]--></p> <p><!--[-->感谢您的支持<!--]--></p> </div> </div> <nav> <ul> <li><a href="###">首页</a></li> <li><a href="###">热搜</a></li> <li><a href="###">联系</a></li> <li><a href="###">关于</a></li> <!--<li><a href="#elements">Elements</a></li>--> </ul> </nav> </header> <!-- Main --> <!-- Footer --> <footer id="footer"> <p class="copyright">© Untitled. Design: <a href="http://www.baidu.com">百度</a>.</p> </footer> </div> <!-- BG --> <div id="bg"></div> <!-- Scripts --> <script src="assets/js/jquery.min.js"></script> <script src="assets/js/skel.min.js"></script> <script src="assets/js/util.js"></script> <script src="assets/js/main.js"></script> </body> </html>要在这段代码的左下角放一个轮播图,代码应该怎么写
05-29

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值