linux 中调用shell脚本时指定工作目录为shell脚本所在的目录
2022/4/27 7:12:52
本文主要是介绍linux 中调用shell脚本时指定工作目录为shell脚本所在的目录,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
1、
root@DESKTOP-1N42TVH:/home/test# ls root@DESKTOP-1N42TVH:/home/test# ls /home/test2/* /home/test2/a.txt /home/test2/test.sh root@DESKTOP-1N42TVH:/home/test# cat /home/test2/test.sh ## shell脚本所在目录及内容 #!/bin/bash wc -l ./a.txt root@DESKTOP-1N42TVH:/home/test# bash /home/test2/test.sh ## 默认的当前目录是执行脚本的目录, 而不是shell所在的目录,执行脚本的目录下没有a.txt wc: ./a.txt: No such file or directory
2、利用
$(dirname $0) ## 指定工作目录为shell脚本所在的目录
修改shell脚本测试:
root@DESKTOP-1N42TVH:/home/test# ls root@DESKTOP-1N42TVH:/home/test# ls /home/test2/* /home/test2/a.txt /home/test2/test.sh root@DESKTOP-1N42TVH:/home/test# cat /home/test2/test.sh #!/bin/bash dir=$(dirname $0) ## 指定工作目录为shlle脚本所在的目录 wc -l $dir/a.txt root@DESKTOP-1N42TVH:/home/test# bash /home/test2/test.sh ## a.txt在shell所在的目录,故可以输出行数 5 /home/test2/a.txt
这篇关于linux 中调用shell脚本时指定工作目录为shell脚本所在的目录的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-03-30[译]漫画SELinux概念
- 2024-03-29linux 移动文件
- 2024-03-28linux .so file
- 2024-03-28Linux 磁盘管理
- 2024-03-28Linux学习笔记(十三)磁盘管理(一):磁盘分区
- 2024-03-26linux 创建 文件
- 2024-03-25使用SecureCRT对Linux vim进行颜色设置
- 2024-03-202019-2020-12 20199317 《Linux内核原理与分析》 第十二周作业
- 2024-03-20Linux运维的第二周总结
- 2024-03-13how to count number of directories in linux