0 bash变量类型:
环境变量
本地变量(局部变量)
位置变量: $1, $2, ... 分别表示第一个参数,第二个参数....
特殊变量
$?: 上个命令执行结果状态码
$#:参数的个数
$*: 参数列表
$@:参数列表
1 变量赋值后引用:
${varname} 其中{}可以省略, 只要在使用时,不会引起混淆那么{}就可以省略
eg:
[root@h2sliver114 local]# name=zm [root@h2sliver114 local]# echo "hello ${name}s." 此时建议加上{}
2 具体变量类型介绍:
本地变量: set VARNAME=VALUE: 作用域为整个bash进程; 局部变量: local VARNAME=VALUE:作用域为当前代码段; 环境变量:作用域为当前shell进程及其子进程; export VARNAME “导出” 位置变量: $1, $2, ... 特殊变量: $?: 上一个命令的执行状态返回值; 程序执行,可能有两类返回值: 程序执行结果 程序状态返回代码(0-255) 0: 正确执行 1-255:错误执行,1,2,127系统预留;
3 查看当前shell中的环境变量:
printenv 这是习惯用到的
env
export
4 位置变量:
eg:
sh filetest.sh /etc/fstab /etc/inittab $1: /etc/fstab $2: /etc/inittab
5 shift使用:
shift 2 表示一次踢出去2个 获取踢出去的这两个中最后的那一个 默认是移动1个
使用案例:
#!/bin/sh # echo "the first param is: $1" shift echo "the second param is: $1" shift echo "the third param is: $1" 结果: [root@chinadaas109 zhoumingtest]# sh fortest.sh 1 2 3 the first param is: 1 the second param is: 2 the third param is: 3
6 变量赋初始值:
${parameter:-word}:如果parameter为空或未定义,则变量展开为“word”;否则,展开为parameter的值; 所谓展开就是直接打印出结果 ${parameter:+word}:如果parameter为空或未定义,不做任何操作;否则,则展开为“word”值; ${parameter:=word}:如果parameter为空或未定义,则变量展开为“word”,并将展开后的值赋值给parameter; ${parameter:offset} 获取从数组脚本offset开始到最后字符的数据 ${parameter:offset:length}:取子串,从offset处的后一个字符开始(角标从0开始计数),取lenth长的子串; 使用案例: [root@chinadaas11 ~]# a=${a:-2} [root@chinadaas11 ~]# echo $a 2 [root@chinadaas11 ~]# echo ${a:=30} 30 [root@chinadaas11 ~]# echo $a 30 [root@chinadaas11 ~]# a='hello world' [root@chinadaas11 ~]# echo ${a:1:4} ello [root@chinadaas11 ~]# echo ${a:1} ello world
7 脚本读取配置文件
一般在如下目录下: /etc/rc.d/init.d/服务脚本 服务脚本支持配置文件:/etc/sysconfig/服务脚本同名的配置文件 如何让脚本来使用配置文件: [root@chinadaas11 zm]# ll total 8 -rw-r--r-- 1 root root 10 Nov 2 14:48 a.conf -rw-r--r-- 1 root root 60 Nov 2 14:54 test.sh [root@chinadaas11 zm]# cat a.conf name='zm' [root@chinadaas11 zm]# cat test.sh #!/bin/bash # . /root/zm/a.conf ---> 等效于 source /root/zm/a.conf 在当前bash环境下读取并执行文件中的命令 [ -n $name ] && echo $name [root@chinadaas11 zm]# sh test.sh zm
相关推荐
高级Bash脚本编程指南 操作系统 - Linux - 高级Bash脚本编程指南.zip
9-11. 利用修改文件名,来转换图片格式 9-12. 模仿getopt 命令 9-13. 提取字符串的一种可选的方法 9-14. 使用参数替换和error messages 9-15. 参数替换和"usage"messages 9-16. 变量长度 9-17. 参数替换中的模式匹配 ...
《Bash 脚本教程》发布了过去三个月,我一直在写《Bash 脚本教程》,现在终于写完了。这个教程是开源的,你可以克隆它的代码仓库,放在本地,也可以提交 iss
何用Python代替Bash脚本指南
gitlab-bash-api:使用bash脚本配置GitLab
以上就是小编为大家带来的解决ssh远程登陆linux显示-bash-4.1$的问题全部内容了,希望大家多多支持软件开发网~ 您可能感兴趣的文章:浅谈linux中shell变量$#,$@,$0,$1,$2的含义解释php $_SERVER windows系统与linux...
windows下msys2或git-bash终端主题配置文件。 1. msys2环境,请将配置文件放在它的$HOME路径下,举例:C:\msys64\home\username\ 2. git-bash环境,请将配置文件放在windows系统的用户目录下,举例:C:\Users\...
pysh:Python增强的bash脚本,允许您编写bash脚本,其中包含Python代码的短片段以及在bash和Python之间共享的本地环境。
10.3.1 Shell基础-Bash基本功能-历史命令与补全.mp4 10.3.2 Shell基础-Bash基本功能-别名与快捷键.mp4 10.3.3 Shell基础-Bash基本功能-输入输出...10.6.3 Shell基础-环境变量配置文件-其他配置文件和登录信息.mp4
【shell脚本】shell脚本之概念、运行方式、变量的使用、交互式脚本、运算(shell脚本基础学习一) shell概念 --- 编写执行--- 变量 --- 运算--- shell脚本 /bin/bash 默认解释器 cat /etc/shells 查看所有解释器 ...
Linux bash入门教程!主要用于学习Linux bash脚本编程,提高Linux使用效率!英文版!
简单的Bash脚本到Windows批处理文件转换器将bash脚本( *.sh )转换为Windows批处理文件( *.bat )。 这不应该是一个完全完整/正确的工具,而是转换脚本时的起点。 对于简单的脚本,这可能会创建正确的结果。 在线...
Dropbox Uploader一个Bash脚本可用于上传、下载、取出或删除来自Dropbox的文件
Shinatra - 只用55行bash脚本实现的一个简单的web服务器
bitbake-bash-完成Bitbake bash 完成脚本如何使用它? 只需将“bitbake”文件复制到/etc/bash_completion.d:cp bitbake /etc/bash_completion.d/它是如何工作的? 它将在第一次执行时在构建目录中创建 2 个隐藏文件...
Advanced-Bash-Scripting-Guide-in-Chinese 《高级Bash脚本编程指南》revision 10 中文第10版
bashblog - 一个Bash脚本用于创建博客。下载、运行和写作。
安装您可以简单地下载脚本文件并授予可执行文件权限。 curl -0 ...
学习bash-shell-script 学习BASH Shell脚本