shell"没有这样的文件或目录"关于变量执行结果

时间:2017-01-22 23:07:23

标签: sh

一些基本功能,目标是通过xpath提取xml var:

function get_xml_value_from_config_dir {
 local src_root=$1
 local xpath_expr="//$2/text()"
 local path_to_local="$src_root/app/etc/local.xml"
 if [ ! -f $path_to_local ]; then echo "Config file not found: $path_to_local"; exit; fi;
 echo $("$xmllint --nocdata --xpath '$xpath_expr' $path_to_local")
}

## and then
src_usr=$(get_xml_value_from_config_dir $src_dir username)

给了我

  

第34行:/ usr / bin / xmllint --nocdata --xpath' //用户名/文本()' /tmp/bin/app/etc/local.xml:没有这样的文件或目录

为什么呢? (/usr/bin/xmllint存在以及/tmp/bin/app/etc/local.xml

1 个答案:

答案 0 :(得分:1)

它告诉你它无法找到名为

的文件或目录
$ ls -al data
total 16
drwxr-xr-x  4 bmitch bmitch 4096 Jun  8  2016 .
drwxr-xr-x 12 bmitch bmitch 4096 Jan 22 20:13 ..
-rw-r--r--  1 bmitch bmitch    0 Jun  8  2016 1
-rw-r--r--  1 bmitch bmitch    0 Jun  8  2016 2
drwxr-xr-x  2 bmitch bmitch 4096 Jun  8  2016 a
drwxr-xr-x  2 bmitch bmitch 4096 Jun  8  2016 b

$ id
uid=1000(bmitch) gid=1000(bmitch) groups=1000(bmitch),24(cdrom),27(sudo),120(bluetooth),127(vboxusers),999(docker)

$ docker run -v `pwd`/data:/data -u 1000 -it --rm busybox

/ $ ls -al /data
total 16
drwxr-xr-x    4 1000     1000          4096 Jun  8  2016 .
drwxr-xr-x   19 root     root          4096 Jan 23 10:24 ..
-rw-r--r--    1 1000     1000             0 Jun  8  2016 1
-rw-r--r--    1 1000     1000             0 Jun  8  2016 2
drwxr-xr-x    2 1000     1000          4096 Jun  8  2016 a
drwxr-xr-x    2 1000     1000          4096 Jun  8  2016 b

/ $ echo 'hello from inside the container' >/data/inside-container.txt

/ $ ls -al /data
total 20
drwxr-xr-x    4 1000     1000          4096 Jan 23 10:25 .
drwxr-xr-x   19 root     root          4096 Jan 23 10:24 ..
-rw-r--r--    1 1000     1000             0 Jun  8  2016 1
-rw-r--r--    1 1000     1000             0 Jun  8  2016 2
drwxr-xr-x    2 1000     1000          4096 Jun  8  2016 a
drwxr-xr-x    2 1000     1000          4096 Jun  8  2016 b
-rw-r--r--    1 1000     root            32 Jan 23 10:25 inside-container.txt

/ $ cat /data/inside-container.txt
hello from inside the container

/ $ exit

$ ls -al data
total 20
drwxr-xr-x  4 bmitch bmitch 4096 Jan 23 05:25 .
drwxr-xr-x 12 bmitch bmitch 4096 Jan 22 20:13 ..
-rw-r--r--  1 bmitch bmitch    0 Jun  8  2016 1
-rw-r--r--  1 bmitch bmitch    0 Jun  8  2016 2
drwxr-xr-x  2 bmitch bmitch 4096 Jun  8  2016 a
drwxr-xr-x  2 bmitch bmitch 4096 Jun  8  2016 b
-rw-r--r--  1 bmitch root     32 Jan 23 05:25 inside-container.txt

确实不太可能存在于您的系统上。 替换

/usr/bin/xmllint --nocdata --xpath '//username/text()' /tmp/bin/app/etc/local.xml

echo $("$xmllint --nocdata --xpath '$xpath_expr' $path_to_local")

顺便说一下,这会将所有echo $($xmllint --nocdata --xpath "$xpath_expr" $path_to_local) 输出放在一行上;要避免这种情况,只需使用

即可
xmllint