linux shell编程

本文介绍了Linux Shell编程的基础知识,包括脚本编写、函数、文本流重定向、变量类型及管理,以及条件判断和循环控制。通过示例讲解了如何使用Shell Bash进行脚本开发,如利用退出状态$?进行逻辑判断,以及如何进行输出重定向。
摘要由CSDN通过智能技术生成

脚本编程

what > why > how

shell bash

解释器 启动器
解释器:
用户交互输入
文本文件输入
脚本本质:可以在脚本文件第一行写 下面的这些语句
– #!/bin/bash
– #! /usr/bin/python

读取方式:
– 当前shell :source/.
– 新建子shell: /bin/bash file / ./file.sh 给文件增加执行权限 chmod +x file.sh

函数

一切皆命令

[root@node001 ~]# xxxxx(){               定义一个函数
> echo "helloworld"
> ls -l
> echo $$
> }
[root@node001 ~]# xxxxx                  使用这个函数
helloworld
total 134152
-rw-------.  1 root  root        900 Aug 29 05:42 anaconda-ks.cfg
-rw-r--r--   1 root  root    8816567 Aug 31 08:17 apache-tomcat-7.0.61.tar.gz
-rw-r--r--   1 root  root        135 Aug 31 05:20 awk.txt
-rw-r--r--   1 root  root        884 Aug 30 09:52 inittab
-rw-r--r--.  1 root  root       8815 Aug 29 05:42 install.log
-rw-r--r--.  1 root  root       3384 Aug 29 05:41 install.log.syslog
-rw-r--r--   1 root  root  126857158 Aug 31 08:17 jdk-7u67-linux-x64.rpm
-rw-r--r--   1 root  root        856 Aug 30 10:09 passwd
drwxr-xr-x   2 root  root       4096 Aug 31 15:57 shell
drwxr-xr-x  12 50469 users      4096 Aug 31 08:22 tengine-2.1.0
-rw-r--r--   1 root  root    1653240 Aug 31 08:17 tengine-2.1.0.tar.gz
8323
[root@node001 ~]# type xxxxx                查看这个函数
xxxxx is a function
xxxxx () 
{ 
    echo "helloworld";
    ls --color=auto -l;
    echo $$
}

文本流重定向

重定向不是命令
程序自身都有I/O
0:标准输入
1:标准输出
2:错误输出
控制程序I/O位置
一切皆文件
/proc/$$/fd
程序是否处理I/O?
绑定顺序:从左到右

创建一个Io
cd /proc/$$/fd
exec 6<> /dev/tcp/www.baidu.com/80
exec 7>& 1

输出重定向 >
		>> 累加
		>&   重定向到指定的 IO编号
l
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值