Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check If Shell Script $1 Is Absolute Or Relative Path [duplicate]

Tags:

bash

shell

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?

like image 694
Dragos Rizescu Avatar asked Nov 25 '13 22:11

Dragos Rizescu


People also ask

What is $? == 0 in shell script?

$? 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.

How can you tell if a path is relative or absolute?

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.

What is $1 $2 in shell script?

$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 a way to validate if a path is absolute/relative?

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.)

What is the absolute path of a folder in Linux?

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

What does-path mean in the validatescript attribute in JavaScript?

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.

Does it matter where I put a bash script?

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.


4 Answers

[ ... ] 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
like image 86
chepner Avatar answered Oct 08 '22 07:10

chepner


Just test on the first character:

if [ "${DIR:0:1}" = "/" ]
like image 34
thom Avatar answered Oct 08 '22 09:10

thom


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
like image 8
loshad vtapkah Avatar answered Oct 08 '22 07:10

loshad vtapkah


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.

like image 6
that other guy Avatar answered Oct 08 '22 08:10

that other guy