As the title says, I am trying to determine if my bash script receives a full path or a relative file to a directory as a parameter.
For some reasons the following doesn't seem to work for me:
#!/bin/bash
DIR=$1
if [ "$DIR" = /* ]
then
echo "absolute"
else
echo "relative"
fi
When I run my script with either a full path or absolute path it says:
./script.sh: line 5: [: too many arguments
relative
For some reasons I can't seem to figure this bug. Any ideas?
$? is the exit status of the most recently-executed command; by convention, 0 means success and anything else indicates failure. That line is testing whether the grep command succeeded. The grep manpage states: The exit status is 0 if selected lines are found, and 1 if not found.
In simple words, an absolute path refers to the same location in a file system relative to the root directory, whereas a relative path points to a specific location in a file system relative to the current directory you are working on.
$0 is the name of the script itself (script.sh) $1 is the first argument (filename1) $2 is the second argument (dir1) $9 is the ninth argument.
Is there anything in the .NET Framework that can help me validate if a path is absolute or relative? It could be as simple as parsing the path to determine if it starts with a drive letter or not. (Yes, I could create a regex or other construct, but I'm wondering if there is a Framework class/type/member that can already do this.)
That path always refers to /home/user/folder2, so it is an absolute path. ..isn't just a shell shortcut; it's a very real entry in the file system for the parent of the directory it is contained in. – chepner
When you use the ValidateScript attribute in the function parameters, the value of -Path is first validated to check if the path is absolute or not before running the actual function code.
Bash shell path relative to current script If you bash script then it is a good idea to make sure it does not matter where you put the script and what is your current working directory when you run it. If you have a large project written using bash then it is even more important to make it directory independent.
[ ... ]
doesn't do pattern matching. /*
is being expanded to the contents of /
, so effectively you have
if [ "$DIR" = /bin /boot /dev /etc /home /lib /media ... /usr /var ]
or something similar. Use [[ ... ]]
instead.
if [[ "$DIR" = /* ]]; then
For POSIX compliance, or if you just don't have a [[
that does pattern matching, use a case
statement.
case $DIR in
/*) echo "absolute path" ;;
*) echo "something else" ;;
esac
Just test on the first character:
if [ "${DIR:0:1}" = "/" ]
One more case is paths started from ~
(tilde). ~user/some.file
or ~/some.file
are some kind of absolute paths.
if [[ "${dir:0:1}" == / || "${dir:0:2}" == ~[/a-z] ]]
then
echo "Absolute"
else
echo "Relative"
fi
ShellCheck automatically points out that "[ .. ] can't match globs. Use [[ .. ]] or grep.
"
In other words, use
if [[ "$DIR" = /* ]]
This is because [
is a regular command, so /*
is expanded by the shell beforehand, turning it into
[ "$DIR" = /bin /dev /etc /home .. ]
[[
is handled specially by the shell, and doesn't have this problem.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With