• shell程序介绍

  1.查看我们的Linux(centos6.5为例)有多少我们可以使用的shell:

[root@localhost bin]# cat /etc/shells
/bin/sh
/bin/bash
/sbin/nologin
/bin/dash
/bin/tcsh
/bin/csh

  系统某些服务在运作过程中,会去检查使用者能够使用的shells,而这些shell的查询就是由/etc/shells这个档案。

  2.当我们登入Linux系统的时候,系统就会给我一个shell来工作,而这个登录取得的shell就记录在/etc/passwd这个档案里:

[root@localhost bin]# cat /etc/passwd
root:x:::root:/root:/bin/bash
bin:x:::bin:/bin:/sbin/nologin
daemon:x:::daemon:/sbin:/sbin/nologin
adm:x:::adm:/var/adm:/sbin/nologin
...

   3.shell的内部指令type,查看指令来自外部指令还是内建在bash当中。

[root@localhost bin]# man cd
[root@localhost bin]# type cd
cd is a shell builtin
[root@localhost bin]# type -t cd
builtin #表示该指令为bash内建的指令功能
[root@localhost bin]# type -a cd
cd is a shell builtin
[root@localhost bin]# type type
type is a shell builtin
[root@localhost bin]# type it ls
alias #表示该指令为命名别名所设定的名称
[root@localhost bin]# type uname
uname is hashed (/bin/uname)
[root@localhost bin]# type -t uname
file   #表示为外部指令

  4.变量的取用 echo

[root@localhost bin]# echo $PATH
/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
[root@localhost bin]# echo ${PATH}
/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin

  变量的设定 = ,如果一个变量未设定,内容为空

[root@localhost bin]# echo $myname

[root@localhost bin]# myname=tian
[root@localhost bin]# echo $myname
tian

  子程序,就是在目前这个shell的情况下,去启用另一个新的shell,新的shell就是子程序。在一般状态下,父程序的自定义变量无法在子程序内使用,但是通过export将变量变成环境变量,就能在子程序下应用了。

[root@localhost bin]# echo $name
yes
[root@localhost bin]# bash #进入所谓的子程序
[root@localhost bin]# echo $name [root@localhost bin]# exit #离开子程序
exit
[root@localhost bin]# export name
[root@localhost bin]# bash
[root@localhost bin]# echo $name
yes
[root@localhost bin]# exit

  5.变量的设定规则:

  6.环境变量

  env,environment的简写,列出所有的环境变量

[root@localhost /]# env
HOSTNAME=localhost.localdomain
SHELL=/bin/bash
TERM=xterm
HISTSIZE=
USER=root
...

  set,观察所有变量(包含环境变量和自定义变量)

[root@localhost /]# set
BASH=/bin/bash
BASH_VERSINFO=([]="" []="" []="" []="" []="release" []="i386-redhat-linux-gnu")
BASH_VERSION='4.1.2(1)-release'
HISTFILE=/root/.bash_history
HISTFILESIZE=
HISTSIZE=
HOSTTYPE=i386
OLDPWD=/
OSTYPE=linux-gnu
PPID=
PS1='[\u@\h \W]\$ '
...

  PS1:提示字符的设定

[root@localhost php]# PS1='[\u@\h \w]\$'
[root@localhost /usr/local/php]#ls

  $变量,代表的是目前这个shell的线程代号,即PID(Process ID)

[root@localhost /]#echo $$

  ?变量:上一个执行的指令所回传的值,如果执行成功,则回传一个0值,否则,就会回传错误代码(一般为非0值)

[root@localhost scripts]# ls
sh01.sh sh02-.sh sh02.sh sh03.sh sh04.sh
[root@localhost scripts]# echo $?

  7.数据流重导向

  数据流重导向可以将standard output与standard error output分别传送到其他档案或装置去,而分别传送所用的特殊字符则如下所示:

[tianxintian22@localhost ~]$ find /home -name .bashrc
find: `/home/w002': Permission denied <==Standard error
/home/tianxintian22/.bashrc <==Standard output
find: `/home/w001': Permission denied <==Standard error

#将stdout与stderr分别存到不同的档案
[tianxintian22@localhost ~]$ find /home -name .bashrc > list_right > list_error
[tianxintian22@localhost ~]$ ls
Desktop Documents Downloads list_error list_right Music Pictures Public Templates Videos
#将错误数据丢弃,屏幕显示正确的数据
#/dev/null 垃圾桶黑暗装置,可以吃掉任何导向这个装置的信息
[tianxintian22@localhost ~]$ find /home -name .bashrc > /dev/null
/home/tianxintian22/.bashrc
#将指令的数据全部写入list档案中
[tianxintian22@localhost ~]$ find /home -name .bashrc > list >&
#[tianxintian22@localhost ~]$ find /home -name .bashrc &> list 这句等同于上一句
[tianxintian22@localhost ~]$ ls
Desktop Documents Downloads list list_error list_right Music Pictures Public Templates Videos
[tianxintian22@localhost ~]$ cat list
find: `/home/w002': Permission denied
/home/tianxintian22/.bashrc
find: `/home/w001': Permission denied
  • shell script

  1.路径与指令搜寻顺序

  2.script指令下达

  以上方式下达脚本时,script都会使用一个新的bash环境来执行脚本内的指令,也就是说,script是在子程序的bash内执行的。

  3.指令依序执行关系

#不清楚/tmp/abc是否存在,但是要建立/tmp/abc/hehe档案
[root@localhost ~]ls /tmp/abc || mkdir /tmp/abc && touch /tmp/abc/hehe

  4.test指令的测试功能

[root@localhost scripts]# ls
sh01.sh sh02-.sh sh02.sh sh03.sh sh04.sh
[root@localhost scripts]# test -e sh01.sh #该档名是否存在
[root@localhost scripts]# echo $? [root@localhost scripts]# test -f sh01.sh #该档名是否存在且为档案(file)
[root@localhost scripts]# echo $? [root@localhost scripts]# test -d sh01.sh #该文件名是否存在且为目录
[root@localhost scripts]# echo $? [root@localhost scripts]# test -d ~/scripts
[root@localhost scripts]# echo $?

  5.判断符号[ ]

  编写脚本:sh01.sh

[root@localhost scripts]# vim sh01.sh
#!/bin/bash
#Program
# This program shows the user's choice
#History
#// tianxintian22 first release
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
read -p "Please input (Y/N)" choice
[ "$choice" == 'Y' -o "$choice" == 'y' ] && echo "OK,continue" && exit 0 # -o 等价于或者(or)
[ "$choice" == 'n' -o "$choice" == 'N' ] && echo "Oh,interrupt" && exit
echo "I don't know what your choice is" && exit

  执行脚本:

[root@localhost scripts]# sh sh01.sh
Please input (Y/N)Y
OK,continue

  6.shell script 默认变数($1,$2...)

[root@localhost scripts]# vim sh02.sh
#!/bin/bash
#program
# program shows the script name, parameters...
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
echo "The script name is ==> $0"
echo "Total parameter number is ==> $#"
[ "$#" -lt ] && echo "The number of parameter is less than 2.Stop here." && exit
echo "Your whole parameter is ==>'$@'"
echo "The first parameter is ==>$1"
echo "The second parameter is ==>$2"
exit

  执行脚本:

[root@localhost scripts]# ./sh02.sh
The script name is ==> ./sh02.sh
Total parameter number is ==>
The number of parameter is less than .Stop here.
[root@localhost scripts]# ./sh07.sh one two
The script name is ==> ./sh02.sh
Total parameter number is ==>
Your whole parameter is ==>'one two'
The first parameter is ==>one
The second parameter is ==>two

  7.条件判断式 if...then

[root@localhost scripts]# vim sh01-.sh
#!/bin/bash
#Program
# This program shows the user's choice
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
read -p "Please input (Y/N):" choice
if [ "$choice" == 'Y' ] || [ "$choice" == 'y' ];then
echo "OK,continue"
elif [ "$choice" == 'n' ] || [ "$choice" == 'N' ];then
echo "Oh,interrupt"
else
echo "I don't know what your choice is"
fi

  执行脚本:

[root@localhost scripts]# ./sh01-.sh
Please input (Y/N):n
Oh,interrupt

  8.case...esac 判断

[root@localhost scripts]# vim sh02.sh
#!/bin/bash
#program
# Show "hello" from $ by using case...esac
#history
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
case $ in
  "hello")
  echo "Hello,how are you?"
  ;;
"")
  echo "You must input parameter, ex> {$0 someword}"
  ;;
*)
  echo "Usage $0 {hello}"
  ;;
esac

  执行脚本:

[root@localhost scripts]# ./sh02.sh hello
Hello,how are you?
[root@localhost scripts]# ./sh02.sh test
Usage ./sh02.sh {hello}
[root@localhost scripts]# ./sh02.sh
You must input parameter, ex> {./sh02.sh someword}

  9.function功能

[root@localhost scripts]# vim sh02-.sh
#!/bin/bash
#program
# use function to repeat information
#history
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
function printit(){
echo -n "Your choice is "
}
echo "This program will print your selection!"
case $ in
"one")
printit;echo $ | tr 'a-z' 'A-Z'
;;
"two")
printit;echo $ | tr 'a-z' 'A-Z'
;;
"three")
printit;echo $ | tr 'a-z' 'A-Z'
;;
*)
echo "Usage $0 {one|two|three}"
;;
esac

  执行脚本:

[root@localhost scripts]# ./sh02-.sh
This program will print your selection!
Usage ./sh02-.sh {one|two|three}
[root@localhost scripts]# ./sh02-.sh one
This program will print your selection!
Your choice is ONE

shell script 的执行方式是由上而下,由左而右,因此在shell script当中的设定一定要写在程序的最前面!

[root@localhost scripts]# vim sh02-.sh
#!/bin/bash
#program
# use function to repeat information
#history
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
function printit(){
echo "Your choice is $1"
}
echo "This program will print your selection!"
case $ in
"one")
printit
;;
"two")
printit
;;
"three")
printit
;;
*)
echo "Usage $0 {one|two|three}"
;;
esac

  执行脚本:

[root@localhost scripts]# ./sh02-.sh
This program will print your selection!
Usage ./sh02-.sh {one|two|three}
[root@localhost scripts]# ./sh02-.sh one
This program will print your selection!
Your choice is

  10.while do done;until do done

#当condition条件满足时,就进行循环
while [ condition ]
do
程序语句
done
#当condition条件成立时,终止循环
until [ condition ]
do
程序语句
done

  计算1+2+3+4+...+100:

#!/bin/bash
#program calculate '1+2+3+4+...+100'
#history ...
sum=
i=
while [ "$i" != '' ]
do
i=$(($i+))
sum=$(($sum+$i))
done
echo "1+2+3+...+100="$sum
#!/bin/bash
#program calculate '1+2+3+4+...+100'
#history
sum=
i=
until [ "$i" == '' ]
do
i=$(($i+))
sum=$(($sum+$i))
done
echo "1+2+3+...+100="$sum

  11.for do done(固定循环)

for var in con1 con2 con3 ...
do
程序段
done

  $var 的变量内容在循环时:第一次循环时,$var的内容为con1;第二次循环时,$var的内容为con2;第三次循环时,$var内容为con3;...

  eg1:

#!/bin/bash
#program
# using for ... loop to print animals
#history
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
for animal in dog cat elephant
do
echo "There are ${animal}s..."
done

  执行脚本:

[root@localhost scripts]# sh sh04.sh
There are dogs...
There are cats...
There are elephants...

  eg2:

#!/bin/bash
users=$(cut -d ':' -f /etc/passwd)
for user in $users
do
id $user
done

  执行脚本:

[root@localhost scripts]# sh sh05.sh
uid=(root) gid=(root) groups=(root)
uid=(bin) gid=(bin) groups=(bin),(daemon),(sys)
uid=(daemon) gid=(daemon) groups=(daemon),(bin),(adm),(lp)
uid=(adm) gid=(adm) groups=(adm)
...

  12.for...do...done

for((初始值;限制值;执行步阶))
do
程序段
done
[root@localhost scripts]# cat sh06.sh
#!/bin/bash
#program
# try do calculate +++...+${your_input}
#history
read -p "input a number:" number
sum=
for((i=;i<=number;i++))
do
sum=$(($sum+$i))
done
echo "1+2+3+...+$number="$sum

  执行脚本:

[root@localhost scripts]# sh sh06.sh
input a number:
+++...+=

  13.debug

[root@localhost scripts]# sh -n sh06.sh
#如果语法没有问题,则不会显示任何信息 [root@localhost scripts]# sh -v sh06.sh #执行脚本前,现将脚本内容输出到屏幕上
#!/bin/bash
#program
# try do calculate +++...+${your_input}
#history
read -p "input a number:" number
input a number:
sum=
for((i=;i<=number;i++))
do
sum=$(($sum+$i))
done
echo "1+2+3+...+$number="$sum
+++...+= [root@localhost scripts]# sh -x sh06.sh #将使用到的script内容显示到屏幕上
+ read -p 'input a number:' number
input a number:
+ sum=
+ (( i= ))
+ (( i<=number ))
+ sum=
+ (( i++ ))
+ (( i<=number ))
+ sum=
+ (( i++ ))
+ (( i<=number ))
+ sum=
+ (( i++ ))
+ (( i<=number ))
+ sum=
+ (( i++ ))
+ (( i<=number ))
+ sum=
+ (( i++ ))
+ (( i<=number ))
+ echo +++...+=
+++...+=

linux shell程序的更多相关文章

  1. Linux Shell 程序调试

    Linux Shell 程序调试 Shell程序的调试是通过运行程序时加入相关调试选项或在脚本程序中加入相关语句,让shell程序在执行过程中显示出一些可供参考的“调试信息”.当然,用户也可以在she ...

  2. 如何运行linux shell程序

    原文地址:http://www.sohu.com/a/138822796_610671 首先,我们从一个十分简单的例子test.sh开始吧: #!/bin/sh #this is a test. cd ...

  3. Linux shell程序一

    设计一个Shell程序,在/$HONE/test目录下建立50个目录,即user1-user50, 并设置每个目录的权限,其中其他用户的权限为:读:文件所有者的权限为: 读.写.执行:文件所有者所在组 ...

  4. linux shell程序常用功能

    一.循环读取文件 循环读取文件方式有多种,推荐下列方法 while read line;do local include=$(echo ${line} | grep "filter" ...

  5. linux c程序中获取shell脚本输出的实现方法

    linux c程序中获取shell脚本输出的实现方法 1. 前言Unix界有一句名言:“一行shell脚本胜过万行C程序”,虽然这句话有些夸张,但不可否认的是,借助脚本确实能够极大的简化一些编程工作. ...

  6. Linux Shell 之 我的第一个Shell程序

      这里我首先会介绍一个Shell是什么,再介绍我的第一个Shell程序和从中总结的经验. 一.Shell是什么 在说我的这个Shell程序之前,还是先跟大家说说什么是Shell吧,相信Shell这个 ...

  7. linux C程序中获取shell脚本输出(如获取system命令输出)

    转载自 http://blog.csdn.net/hjxhjh/article/details/7909518 1. 前言 Unix 界有一句名言:“一行shell脚本胜过万行C程序”,虽然这句话有些 ...

  8. linux系统编程综合练习-实现一个小型的shell程序(四)

    上节中已经对后台作业进行了简单处理,基本上要实现的功能已经完了,下面回过头来,对代码进行一个调整,把写得不好的地方梳理一下,给代码加入适当的注释,这种习惯其实是比较好了,由于在开发的时候时间都比较紧, ...

  9. linux系统编程综合练习-实现一个小型的shell程序(一)

    之前已经花了不少篇幅学习了linux系统编程的很多知识点:文件与io.进程.信号.管道,而零散的知识点,怎么能够综合的串接起来是学习的一个很重要的目的,当然最好的方式就是用所学的知识点做一个项目了,所 ...

随机推荐

  1. C#中,双屏/两屏/三屏/多屏跳转判断

    之前伤脑筋写过一次在Web中,JS,ActiveXObject去读取显示器数量.分辨率去判断单双屏跳转. 那么在客户端中,用C#去读取硬件信息,更方便更容易! 思路参考代码: ) { //此显示器是否 ...

  2. Jquery网页元素里面的操作以及JSON

    如果网页里面有复选框,下拉列表Jquery怎么来操作,主要是怎么选取数据,就是取选中值,第二个是设置哪一项选中 <script src="jquery-1.11.2.min.js&qu ...

  3. Windows下MySQL的常用操作

    1.MySQL关闭与重启 1.MYSQL服务 我的电脑——(右键)管理——服务与应用程序——服务——MYSQL——开启(停止.重启动) 2.如果你没安装系统服务,可在命令行模式定位到mysql下的bi ...

  4. Linux epoll

    一. epoll函数集 epoll主要有三个函数: 1. int epoll_create(int size); 创建一个epoll的句柄,size用来告诉内核这个监听的数目一共有多大.这个参数不同于 ...

  5. .NET Core的文件系统[4]:由EmbeddedFileProvider构建的内嵌(资源)文件系统

    一个物理文件可以直接作为资源内嵌到编译生成的程序集中.借助于EmbeddedFileProvider,我们可以统一的编程方式来读取内嵌于某个程序集中的资源文件,不过在这之前我们必须知道如何将一个项目文 ...

  6. J2EE 项目读写分离

    先回答下 1.为啥要读写分离? 大家都知道最初开始,一个项目对应一个数据库,基本是一对一的,但是由于后来用户及数据还有访问的急剧增多, 系统在数据的读写上出现了瓶颈,为了让提高效率,想读和写不相互影响 ...

  7. 图的生成树(森林)(克鲁斯卡尔Kruskal算法和普里姆Prim算法)、以及并查集的使用

    图的连通性问题:无向图的连通分量和生成树,所有顶点均由边连接在一起,但不存在回路的图. 设图 G=(V, E) 是个连通图,当从图任一顶点出发遍历图G 时,将边集 E(G) 分成两个集合 T(G) 和 ...

  8. CSS学习笔记——视觉格式化模型 visual formatting model

    CSS 视觉格式化模型(visual formatting model)是用来处理文档并将它显示在视觉媒体上的机制.他有一套既定的规则(也就是W3C规范),规定了浏览器该怎么处理每一个盒子.以下内容翻 ...

  9. 一款批量修改AE模板的工具

    一.需求分析 对于视频后期剪辑及相关从业人员来说,AE(After Effects)模板效果是一个不错的开始点.在模板效果的基础上,可以很快的做出各种炫酷的后期效果.但是在网上下载的模板工程中,往往包 ...

  10. DOM 事件深入浅出(一)

    在项目开发时,我们时常需要考虑用户在使用产品时产生的各种各样的交互事件,比如鼠标点击事件.敲击键盘事件等.这样的事件行为都是前端DOM事件的组成部分,不同的DOM事件会有不同的触发条件和触发效果.本文 ...