Shell Script and spaces in path

2020-06-21 05:38发布

I have larger shell script which handles different things.

It will get it's own location by the following...

BASEDIR=`dirname $0`/..
BASEDIR=`(cd "$BASEDIR"; pwd)`

then BASEDIR will be used create other variables like

REPO="$BASEDIR"/repo

But the problem is that this shell script does not work if the path contains spaces where it is currently executed.

So the question is: Does exist a good solution to solve that problem ?

标签: shell
7条回答
唯我独甜
2楼-- · 2020-06-21 06:15

Quote your full variable like this:

REPO="$BASEDIR/repo"
查看更多
成全新的幸福
3楼-- · 2020-06-21 06:17

Works perfectly fine for me. How are you using REPO? What specifically "doesn't work" for you?

I tested

#!/bin/sh
BASEDIR=`dirname $0`/..
BASEDIR=`(cd "$BASEDIR"; pwd)`
REPO="$BASEDIR"/repo
echo $REPO

in a ".../a b/c d" directory. It outputs ".../a b/repo", as expected.

Please give the specific error that you are receiving... A "doesn't work" bug report is the least useful bug report, and every programmer absolutely hates it.

查看更多
别忘想泡老子
4楼-- · 2020-06-21 06:18

There is no reliable and/or portable way to do this correctly.

See How do I determine the location of my script? as to why

The best answer is the following, which is still OS dependent

BASEDIR=$(readlink -f $0)

Then you can do things like REPO="$BASEDIR"/repo , just be sure to quote your variables as you did.

查看更多
做个烂人
5楼-- · 2020-06-21 06:28

Using spaces in directory names in unix is always an issue so if they can be avoided by using underscores, this prevents lots of strange scripting behaviour.

I'm unclear why you are setting BASEDIR to be the parent directory of the directory containing the current script (..) and then resetting it after changing into that directory

The path to the directory should still work if it has ..

e.g. /home/trevor/data/../repo

BASEDIR=`dirname $0`/..

I think if you echo out $REPO it should have the path correctly assigned because you used quotes when assigning it but if you then try to use $REPO somewhere else in the script, you will need to use double quotes around that too.

e.g.

#!/bin/ksh

BASEDIR=`dirname $0`/..
$REPO="$BASEDIR"/repo

if [ ! -d ["$REPO"] 
then
  echo "$REPO does not exist!"
fi
查看更多
一夜七次
6楼-- · 2020-06-21 06:29

Use speech marks as below:

 BASEDIR=`dirname "${0}"`/..
查看更多
做自己的国王
7楼-- · 2020-06-21 06:32

The answer is "Quotes everywhere."

If the path you pass in has a space in it then dirname $0 will fail.

$ cat quote-test.sh
#!/bin/sh

test_dirname_noquote () {
        printf 'no quotes: '
        dirname $1
}
test_dirname_quote () {
        printf 'quotes: '
        dirname "$1"
}

test_dirname_noquote '/path/to/file with spaces/in.it'
test_dirname_quote '/path/to/file with spaces/in.it'

$ sh quote-test.sh
no quotes: usage: dirname string
quotes: /path/to/file with spaces

Also, try this fun example

#!/bin/sh

mkdir -p /tmp/foo/bar/baz
cd /tmp/foo
ln -s bar quux
cd quux
cat >>find-me.sh<<"."
#!/bin/sh

self_dir="$(dirname $0)"
base_dir="$( (cd "$self_dir/.." ; pwd -P) )"
repo="$base_dir/repo"

printf 'self: %s\n' "$self_dir"
printf 'base: %s\n' "$base_dir"
printf 'repo: %s\n' "$repo"
.

sh find-me.sh

rm -rf /tmp/foo

Result when you run it:

$ sh example.sh
self: .
base: /tmp/foo
repo: /tmp/foo/repo
查看更多
登录 后发表回答