dash shell 的一些总结
最近写个一些dash shell 相关的代码,中间遇到了一些坑以及需要注意的地方,记录一下
1 参数
numberofargmuments(){
echo "The number of args: " "$#"
# >> 2 参数个数
# $0 当前执行脚本的相对路径、
echo "shell script path:" $0 $1 $2 分别代表第一 第二个参数。。。
获取当前脚本的路径
currentdir1=$(cd $(dirname $0)|pwd)
echo "current dir is(cd dirname pwd)": "$currentdir1"
#说明 $(dirname $0) 获取当前脚本的目录的相对路径,cd 进去 执行pwd获得全路径获取当前脚本的路径
currentdir2=$(dirname $(readlink -f $0))
echo "current dir is(dirname readlink -f)": "$currentdir2"
}
1.1 参数的处理 1
────────
$* 除$0以外的所有参数, 是一个完整的字符串$@ 除$0以外的所有参数, 是一个
数组两者区别可以用下面的代码看出来./prac1.sh arg1 arg2
arguments_deal1(){
echo print '$*'
for arg in "$*";do
echo "$arg"
done
arg1 arg2 输出为一行echo print '$@' for arg in "$@";do echo "$arg"
done输出为2行arg1 arg2 } arguments_deal1 $@ arguments_deal1 $*
1.2 参数处理 2
───────
argment_deal2(){如何取出option可以先判断$1 or $2 然后再取出option if [
"$1" = "-m" ];then echo '$0' is -m and value is: $2 fi #or if [ "$2" =
"-m" ];then echo '$2' is -m and value is: $1 fi }
1.3 参数处理 3
───────
shift 会将参数往左移动一位,也就相当于删除第一个参数eg:./prac1.sh -f
v1 v2 v3 or ./prac1.sh v1 v2 v3 -f arguments_deal3(){ values="" while
[ -n "$1" ];do if [ "$1" = "-f" ];then shift continue else
values="$values $1" fi shift done
echo argments values:"$values" } #arguments_deal3 $@
2 字符串
════════
2.1 字符串截取
───────
var="/user/home/user1" echo ${var#*home/} # 去掉*home/ 留下 user1
2.2 split & count
────────
count=0 vars=$(echo $var | tr '/' ' ') # split for v in ${vars};do
count=`expr $count + 1` # count echo $v done tr commands can referce
here <https://www.cnblogs.com/bingguoguo/articles/9188703.html>
2.3 join
────
joinedstr=""
for v in ${vars};do
if [ "$joinedstr" = "" ]; then
joinedstr=$v
else
joinedstr="$joinedstr#$v"
fi
done
3 目录
══════
3.1 列出绝对路径
────────
files=$(ls | sed "s:^:`pwd`/: ")
for f in $files;
do
echo $file
done
4 函数
══════
4.1 函数的定义函数反回和调用
──────────────
bash可以有关键字function 但是dash没有,一律不使用function 关键字函数
可以返回一些字符串或者数字
funcreturnstr(){
echo "returnstring"
}
if [ $(funcreturnstr) = "returnstring" ] ;then
echo "return ok"
fi
带参数
funcreturnstr(){
if [ "$1" = "1" ];then
echo "return1"
elif [ "$1" = "2" ];then
echo "return2"
else
echo "returnstring"
fi
}
if [ $(funcreturnstr 1) = "return1" ];then
echo "return1 ok"
fi
注意参数$1在比较的时候一定要加双引号,不要问为什么,谁不加谁知道。
5 here string
═════════════
在使用while read 的时候会遇到在while里面的变量无法更改 比如total=0
echo "a,b,c" | tr ',' '\n'|while read x;do
$((total=total+1))
echo in while total:$total
done
echo out total:$total
out total打印始终为0 原因就是 代码是通过管道传送给while read 进程,
total变量被传到了子进程中,而父进程是得不到在子进程中的更改的,要获得
这个更改需要使用here documents
eg:
total=0
while read x;do
total=`expr $total + 1`
echo in while total:$total
done<<EOF
`echo "a,b,c" | tr ',' '\n'`h
EOF
6 读取文件
══════════
文件每行存放的方式为key value 中间是空格,如果要取出key为某一个值的时
候可以这样
while read line;do
第一种方法
key=`echo $line | cut -d ' ' -f1`
if [ "$key" = "key2" ] ;then
echo first method: "$key"
fi
第二中方法
read x y <<EOF $line
EOF
if [ "$x" = "key3" ];then
echo second method:$x , $y
fi
done < tmpfile
7 需要注意的地方
════════════════
7.1 判断文件夹和文件
──────────
if [ -f file ];then echo exists fi if [ -d folder ];then echo exists
fi
if [ -e path ] ;then echo exists fi
7.2 使用参数比较的时候一定要用双引号
──────────────────
shell将参数按字符串解析,如果不用引号括起来,字符串中间有空格的时候,
会出现两个字符串这样就出问题了。
7.3 文件操作的时候需要注意路径,绝对路径或相对路径,要统一
─────────────────────────────
8 参考
══════
<https://www.jianshu.com/p/762d4cccee7e>
<http://blog.chinaunix.net/uid-25266990-id-3268759.html>
<https://www.cnblogs.com/Malphite/p/7742406.html>
<https://www.cnblogs.com/zwgblog/p/6031256.html>
<https://www.cnblogs.com/xuxm2007/p/7554543.html>
<https://www.cnblogs.com/wangtao1993/p/6136894.html>
dash shell 的一些总结的更多相关文章
- Ubuntu下把缺省的dash shell修改为bash shell
Ubuntu下缺省使用的是shell是dash,而不是bash.从/bin/sh软连接的指向可以看出这点. dash shell 虽然比bash shell更轻便,但是它并不支持所有的语法,运行she ...
- Ubuntu下修改缺省dash shell为bash shell
Debian和Ubuntu下缺省使用的是shell是dash,而不是bash.从/bin/sh软连接的指向可以看出这点. 这是一个不同于bash的shell,它主要是为了执行脚本而出现,而不是交互,它 ...
- ubuntu12.04中shell脚本无法使用source的原因及解决方法
现象: shell脚本中source aaa.sh时提示 source: not found 原因: ls -l `which sh` 提示/bin/sh -> dash 这说明是用dash来进 ...
- 《Linux命令行与shell脚本编程大全》 第二十二章 学习笔记
第二十二章:使用其他shell 什么是dash shell Debian的dash shell是ash shell的直系后代,ash shell是Unix系统上原来地Bourne shell的简化版本 ...
- 《Linux命令行与shell脚本编程大全》 第十五章 学习笔记
第十五章:控制脚本 处理信号 重温Linux信号 信号 名称 描述 1 HUP 挂起 2 INT 中断 3 QUIT 结束运行 9 KILL 无条件终止 11 SEGV 段错误 15 TERM 尽可能 ...
- shell初步了解
shell的类型 查看用户所用的shell程序,在/etc/passwd 文件中的第七个字段(好像就是最后一个,主要是bash shell) 还有一个默认shell是/bin/sh,它作为默认的系统s ...
- bash, sh, dash 傻傻分不清楚
原文链接,转载请注明出处: http://www.happycxz.com/m/?p=137 常见shell类型 Bourne shell (sh) UNIX 最初使用,且在每种 UNIX 上都可以使 ...
- 其他shell
dash shell Debian的dash shell的历史很有趣.它是ash shell的直系后代,而ash shell则是Unix系统上原来 的Bourne shell的简化版本(参见第1章). ...
- Linux编程 9 (shell类型,shell父子关系,子shell用法)
一. shell类型 1.1 交互式 bin/ shell程序 当用户登录到某个虚拟控制台终端或是在GUI中启动终端仿真器时,默认的shell程序就会开始运行.系统启动什么样的shell程序取决于你 ...
随机推荐
- Linux之GDB调试命令
gdb启动 gdb 程序名 l 查看源代码(默认显示十行) l 文件名:行数 l 文件名:函数名 添加断点 break + 行数 (b 也行) b 15 if i == 15 条件断点 i b 查看断 ...
- 阿里云Ubuntu安装LNMP环境之Nginx
在QQ群很多朋友问阿里云服务器怎么安装LNMP环境,怎么把项目放到服务器上面去,在这里,我就从头开始教大家怎么在阿里云服务器安装LNMP环境. 在这之前,我们先要知道什么是LNMP. L: 表示的是L ...
- Spring Cloud Eureka(四):Eureka 配置参数说明
Eureka Client 配置项(eureka.client.*) org.springframework.cloud.netflix.eureka.EurekaClientConfigBean 参 ...
- java面试题,转载自http://www.cnblogs.com/nnngu/p/8471043.html#3914167
Java面试题库及答案解析 1.面向对象编程(OOP)有哪些优点? 代码开发模块化,更易维护和修改. 代码复用. 增强代码的可靠性和灵活性. 增加代码的可理解性. 2.面向对象编程有哪些特性? 封 ...
- react中使用map时onClick事件失效
分享一些踩过的坑 react中使用map时onClick事件失效 <span> { count.map( (item,index)=>{ return <span style= ...
- C++代码匈牙利命名规范
一.类 除了异常类等个别情况(不希望用户把该类看作一个普通的.正常的类之情况)外,C++类/结构的命名应该遵循以下准则: C++类的命名 类的名称都要以大写字母“C”开头,后跟一个或多个单词.为 ...
- 数据中心网络架构的问题与演进 — NFV
目录 文章目录 目录 前文列表 前言 NFV NFV 的最终目标 NFV 的抽象框架 基础架构层与虚拟基础设施管理层 资源管理与业务流程编排层 OSS 层 SDN 控制层 NFV 的生态合作 NFV ...
- idea设置包的导入和提示重复代码下波浪线
1.一般idea都不会导入包.即使按了(以下都是已eclipse设置idea的快捷键) alt+enter键也不能导入. 2.关闭重复代码提示(也就是重复代码有波浪线)
- playbook文件内容解释
[root@node1 playbook]# cat nginx.yml - hosts: test \\主机组,要和nginx.yml在同一个目录下 remote_user: root \\远端执行 ...
- Java泛型(4):泛型与匿名内部类
泛型同样也可以使用在匿名内部类中. 下面的例子是对 Java泛型(3):泛型方法 中例(2)的修改. public interface Generator<T> { T next(); } ...