获取shell脚本路径
❶ 如何获得sh或source执行的shell脚本位置
1) 支持从其他目录执行,如 source xxx/yyy/test.sh
1) 支持 sh 和 source 执行
2) 支持路径中有空格
3) 支持 zsh 跟 bash
#!/bin/sh
if [ "$0" = "bash" ]; then
cd "$(dirname "$BASH_SOURCE")"
CUR_FILE=$(pwd)/$(basename "$BASH_SOURCE")
CUR_DIR=$(dirname "$CUR_FILE")
cd - > /dev/null
else
echo "$0" | grep -q "$PWD"
if [ $? -eq 0 ]; then
CUR_FILE=$0
else
CUR_FILE=$(pwd)/$0
fi
CUR_DIR=$(dirname "$CUR_FILE")
fi
echo $CUR_DIR
❷ 如何获得 sh 或 source 执行的shell 脚本位置
1) 支持从其他目录执行,如 source xxx/yyy/test.sh
1) 支持 sh 和 source 执行
2) 支持路径中有空格
3) 支持 zsh 跟 bash
#!/bin/sh
if [ "$0" = "bash" ]; then
cd "$(dirname "$BASH_SOURCE")"
CUR_FILE=$(pwd)/$(basename "$BASH_SOURCE")
CUR_DIR=$(dirname "$CUR_FILE")
cd - > /dev/null
else
echo "$0" | grep -q "$PWD"
if [ $? -eq 0 ]; then
CUR_FILE=$0
else
CUR_FILE=$(pwd)/$0
fi
CUR_DIR=$(dirname "$CUR_FILE")
fi
echo $CUR_DIR
❸ linux shell脚本中如何获取文件的绝对路径readlink可以么
传给函数的路径是/root/file,如果这是函数的第一个参数,那么函数中可以用
$1
获取。
得到文件名file1和file2后,echo
"$1/file1"
就输出/root/file/file1,echo
"$1/file2"
输出/root/file/file2。
❹ shell编程中,如何获取当前执行shell的路径
常见的一种误区,是使用pwd命令,该命令的作用是“print name of current/working directory”,这才是此命令的真实含义,当前的工作目录,这里没有任何意思说明,这个目录就是脚本存放的目录。所以,这是不对的。你可以试试bash shell/a.sh,a.sh 内容是 pwd,你会发现,显示的是执行命令的路径/home/june,并不是 a.sh 所在路径:/home/june/shell/a.sh
另一个误人子弟的答案,是$0,这个也是不对的,这个$0是Bash环境下的特殊变量,其真实含义是:
Expands to the name of the shell or shell script. This is set at shell initialization. If bash is invoked with a file of commands, $0 is set to the name of that file. If bash is started with the -c option, then $0 is set to the first argument after the string to be executed, if one is present. Otherwise, it is set to the file name used to invoke bash, as given by argument zero.
这个$0有可能是好几种值,跟调用的方式有关系:
使用一个文件调用bash,那$0的值,是那个文件的名字(没说是绝对路径噢)
使用-c选项启动bash的话,真正执行的命令会从一个字符串中读取,字符串后面如果还有别的参数的话,使用从$0开始的特殊变量引用(跟路径无关了)
除此以外,$0会被设置成调用bash的那个文件的名字(没说是绝对路径)
- basepath=$(cd `dirname $0`; pwd)
- dirname $0,取得当前执行的脚本文件的父目录
- cd `dirname $0`,进入这个目录(切换当前工作目录)
- pwd,显示当前工作目录(cd执行后的)
下面对比下正确答案:
在此解释下basepath:
由此,我们获得了当前正在执行的脚本的存放路径。
❺ 安卓 怎么获取sh脚本目录并cd到脚本目录
1.修改启动脚本init.rc在最后添加 #===============================add my shellscript service sysinit /system/xbin/busybox run-parts /system/etc/init.d oneshot 上面busybox工具路径要根据自己的系统写 2.在system/etc下创建init.d目录,在此目录下添加写好的脚本去掉后缀.sh,添加可执行权限 3.重启ok
❻ shell获得当前目录名的几种方法
1、我们先登录第一个用户,root。登录后,echo $SHELL。
❼ shell脚本路径问题
关于路径,我的建议是用完整路径,不要用./ ../ 之类的路径。
##################################################
#!/bin/bash
#cat a.properties
FILE=/home/user/A/D/a.properties
read -p "enter your want to look:" ANSWER
if [ -n "$ANSWER ]
then
XX=`cat $FILE |grep $ANSWER`
echo "$XX"
else
echo "enter error!"
fi
##################################################
这个脚本的意思是,让你输入你需要查询的内容,并且把内容显示在屏幕上给你看,如果你输入的内容是空的,就不给查询,并且抛出enter error的错误提示。
❽ linux shell 问题怎么知道shell脚本自己的路径
用dirname命令和pwd命令(或读取PWD变量)组合。先进入脚本所在目录,读出PWD值(这就是需要的东西),然后返回原来的位置[可选]。命令如下:
echo $(cd "$(dirname "$0")"; pwd)
这里的cd是在子shell中执行,所以不需要用额外的命令返回原位置了。
❾ 怎么让shell脚本获得自己所在目录名
1.使用绝对路径执行的shell文件(如/home/xxx/binfile)
直接使用dirname $0即可
2.对于使用相对路径执行的shell文件(如 ./xxx/binfile)
pwd与dirname结合使用;pwd获得的是执行当前shell文件时,用户所在的位置;dirname可以获得相对于那个位置的偏移:
例如某shell文件所在的位置是/home/user_name/work2/SNS3_server_im/Developing/trunk/im_capp/src/notify_serv/shell文件名
1 #!/bin/sh
2 pwd
3 echo `dirname $0`
执行后输出
/home/user_name/work2/SNS3_server_im/Developing/trunk/im_capp/src
./notify_serv