设为首页 收藏本站
查看: 1169|回复: 0

Shell脚本初探

[复制链接]

尚未签到

发表于 2015-12-3 15:02:51 | 显示全部楼层 |阅读模式
一、Shell编程概述

简介
  Shell是一种命令行解释器,为用户和操作系统之间提供通信的一种接口,它接受来自用户输入的命令,并将其转换为一系列的系统调用送到内核执行,并将结果输出给用户。
  Shell也是一种编程工具,称为脚本语言,与编译型语言不同,脚本语言又被称作解释型语言,运行时翻译,执行一条语句时才翻译。
  Shell脚本以#!开头指明解释器的具体位置(#!/bin/bash),其他位置使用#注释;
  运行方式:1、 bash file.sh   2、 ./file.sh  (需要有可执行权限)  3、 file.sh  (可执行权限,当前目录包含在$PATH中)
  调试: bash -x file.sh,在脚本中使用  set -x 和 set +x 可以把需要调试的部分包含进来;

内建命令
  内建命令:bash自身提供的命令,执行过程调用当前Shell进程的一个函数
  外部命令:执行时触发磁盘IO,需要fork出一个单独的进程来执行,执行完再退出


[iyunv@VM_139_12_tlinux ~]# which cd           //内建命令,不是可执行文件
/usr/bin/which: no cd in (/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin)
[iyunv@VM_139_12_tlinux ~]# type cd        //判断是否是内建命令
cd is a shell builtin
[iyunv@VM_139_12_tlinux ~]# type ifconfig    
ifconfig is /sbin/ifconfig
  ------
  执行程序:.(点号)


root@192.168.100.254:/data/redd/test# ./fact.sh 5
bash: ./fact.sh: Permission denied
root@192.168.100.254:/data/redd/test# . ./fact.sh 5
120
root@192.168.100.254:/data/redd/test# . fact.sh 5
120
root@192.168.100.254:/data/redd/test# source ./fact.sh 6     //使用source亦可
720
root@192.168.100.254:/data/redd/test# source fact.sh 6
720
root@192.168.100.254:/data/redd/test#
  ------
  别名:alias


[iyunv@VM_9_26_tlinux /data]# alias      
alias grep='grep --color=auto'
alias l.='ls -d .* --color=auto'
alias ll='ls -l --color=auto'
alias ls='ls --color=auto'
alias vi='vim'
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
[iyunv@VM_9_26_tlinux /data]# cat ~/.bashrc   //在用户家目录的.bashrc中设置可永久生效
# .bashrc
# User specific aliases and functions
#alias rm='rm -i'
#alias cp='cp -i'
#alias mv='mv -i'
# Source global definitions
if [ -f /etc/bashrc ]; then
. /etc/bashrc
fi
if [ "$TERM" != "dumb" ] && [ -x /usr/bin/dircolors ]; then
eval `dircolors /etc/DIR_COLORS`
alias ls='ls --color=auto'
alias grep='grep --color=auto'
fi
[iyunv@VM_9_26_tlinux /data]# alias hello='ll'      //只在当前shell环境下有效
[iyunv@VM_9_26_tlinux /data]# hello
total 156
drwxr-xr-x 2 root root  4096 Aug 13 03:21 container2host
-rwxr-xr-x 1 root root  6677 Dec 31  2014 daily.pl
drwxr-xr-x 3 root root  4096 May 23  2014 dcagent
drwxr-xr-x 4 root root  4096 Jun 23 07:27 home
drwxr-xr-x 4 root root  4096 May 26  2014 install
drwxr-xr-x 3 root root  4096 Jul 27 14:14 lighttpd_logs
drwxr-xr-x 3 root root  4096 Aug 20 15:48 log
drwxr-xr-x 2 root root  4096 Sep  2 05:30 log_copy_dir
drwx------ 2 root root 16384 May 22  2014 lost+found
drwxr-xr-x 4 root root  4096 Sep  2 11:47 release
drwxr-xr-x 2 root root  4096 May 26  2014 setup_rs
-rw-r--r-- 1 root root 15389 Aug 13 03:21 setup_rs_last_log
-rw-r--r-- 1 root root 69543 Aug 13 03:21 setup_rs.log
drwxr-xr-x 3 root root  4096 Aug 20 15:34 stat
drwxr-xr-x 2 root root  4096 Feb  3  2015 temp
[iyunv@VM_9_26_tlinux /data]# unalias hello          //只在当前shell有效
[iyunv@VM_9_26_tlinux /data]# hello      
bash: hello: command not found
[iyunv@VM_9_26_tlinux /data]# alias he='ls'
[iyunv@VM_9_26_tlinux /data]# he
container2host  daily.pl  dcagent  home  install  lighttpd_logs  ......
[iyunv@VM_9_26_tlinux /data]# unalias -a           //删除当前shell环境下所有别名
[iyunv@VM_9_26_tlinux /data]# he
bash: he: command not found
[iyunv@VM_9_26_tlinux /data]#
  ------
  任务前后台切换:bg、fg、jobs
DSC0000.jpg
  ------
  打印: echo


root@192.168.100.254:/data/redd/test# echo "hello word"  //默认换行打印
hello word
root@192.168.100.254:/data/redd/test# echo -n "hello word"  //不换行
hello wordroot@192.168.100.254:/data/redd/test#
root@192.168.100.254:/data/redd/test# echo "hello\tword\n"   //默认不解释打印转义字符
hello\tword\n
root@192.168.100.254:/data/redd/test# echo -e "hello\tword\n" //解释转义字符
hello   word
root@192.168.100.254:/data/redd/test#
  ------
  将参数作为shell的输入并执行命令:eval
DSC0001.jpg
  ------
  执行命令以取代当前的shell:exec
  内建命令exec不会启动新的Shell,只是用要被执行的命令替换当前的Shell进程,并且将老进程的环境清理掉,而且exec命令后的其他命令将不再执行。为了避免这种情况,将exec命令放到脚本中,由主脚本去调用这个脚本,在子脚本中执行到exec后,该子脚本进程就被替换成相应的exec的命令。
  另,source命令和点号不会为脚本新建shell,只是将脚本包含的命令在当前shell执行。
DSC0002.jpg    DSC0003.jpg
  ------
  变量导出:export
  用户登录到系统后,将启动一个shell,可以创建并运行脚本。通常,登录shell是父shell,则在该脚本中运行的shell是子shell。父shell中创建变量时,这些变量并不会被其子shell进程所知,即变量默认情况下是私有的,使用export可以将变量导出,使子shell也可以使用该变量。
  注意,子shell中对该变量的修改并不会影响父shell,即变量导出相当于值传递。
  ------
  从标准输入读取一行到变量:read


root@192.168.100.254:/root# read      
23
root@192.168.100.254:/root# echo $REPLY
23
root@192.168.100.254:/root# read      
hello world
root@192.168.100.254:/root# echo $REPLY
hello world
root@192.168.100.254:/root# read -p "enter a value:" N
enter a value:ok
root@192.168.100.254:/root# echo $N
ok
root@192.168.100.254:/root#
  ------
  向左移动位置参数:shift
  脚本在运行时可接受参数,第一个参数为$1,第二个为$2......,第n个为$n,所有参数记作$@或$*,参数总个数记作$#,脚本名记作 $0
  另,$@和$*的区别
  在不用双引号包含时,都是一样的,以IFS划分字段
  在使用双引号包含时,$@以IFS(默认空格)划分各个参数,$*将所有参数视作一个整体



#!/bin/bash
until  [ -z "$1" ]
do
echo "$@ "
shift   
done
  运行之


root@192.168.100.254:/data/redd/test# . shift.sh A B C
A B C
B C
C
root@192.168.100.254:/data/redd/test#
  ------
  控制shell执行程序的资源:ulimit

  Provides control over the resources available to the shell and to processes started by it, on systems that allow such control.用来控制shell及它所产生的进程的资源。
  The value of limit can be a number in the unit specified for the resource or one of the special values hard, soft, or unlimited, which stand for the current hard limit, the current soft limit, and no limit, respectively.



root@192.168.100.254:/data/redd/test# ulimit -a
core file size          (blocks, -c) 0           //core文件上限
data seg size           (kbytes, -d) unlimited  //数据段最大值
scheduling priority             (-e) 0      //调度优先级
file size               (blocks, -f) unlimited  //创建文件的最大值
pending signals                 (-i) 62822    //挂起的信号数量
max locked memory       (kbytes, -l) 64         //最大锁定内存值
max memory size         (kbytes, -m) unlimited  //最大可用常驻内存值
open files                      (-n) 100001     //最大打开文件数
pipe size            (512 bytes, -p) 8      //管道最大缓冲区的值
POSIX message queues     (bytes, -q) 819200   //消息队列的最大值
real-time priority              (-r) 0      //程序的实时性优先级
stack size              (kbytes, -s) 10240    //栈大小
cpu time               (seconds, -t) unlimited  //最大CPU占用时间
max user processes              (-u) 1024     //用户最大进程数目
virtual memory          (kbytes, -v) unlimited    //最大虚拟内存
file locks                      (-x) unlimited    //文件锁
  一般用法


ulimit -HSTabcdefilmnpqrstuvx [limit]  //不带limit查看对应的值,-S设置软限制,-H设置硬限制,默认同时设置
ulimit -c ulimited           //设置最大core文件大小
ulimit -S -n 4096            //单独设置软限制
  二、Bash Shell的安装
  三、Shell编程基础
  四、测试和判断
  五、循环
  六、函数
  七、重定向
  八、脚本范例
  来源:Linux系统命令及Shell脚本实践指南
  雷锋:LinuxTone.Org:::>/ebooks/

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-146912-1-1.html 上篇帖子: Bash Shell编程要点小结 下篇帖子: Shell 调试
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表