Pretty self explanatory.
The first argument should be:
- Checked if the file exists
- Echo that file's absolute path
For example:
+akiva@akiva-ThinkPad-X230:~$ ./myscript myfile.txt
/home/akiva/myfile.txt
Thanks
Pretty self explanatory.
The first argument should be:
For example:
+akiva@akiva-ThinkPad-X230:~$ ./myscript myfile.txt
/home/akiva/myfile.txt
Thanks
Script is not necessary. A single
readlink
command is sufficient:From the
man readlink
:If your script is
And has execute permission (
chmod u+x scriptname
) You can enterTo get the full path if the file exists (although Serg is right that the test is redundant if we use
readlink -e
and George is right to userealpath
rather thanreadlink
)Notes
[[ -e "$1" ]]
test whether$1
, the first argument to the script, exists&&
if it does (if the previous command was successful) then do the next commandreadlink -f -- "$1"
print the full path (to the real file, even if$1
is a symlink)OP requested special characters be printed with escapes. There must be a smart way* but I did it like this (although it won't deal with single quotes - but those cannot be escaped anyway)
If it's only spaces you're worried about, you could make that
This would get single quotes (not very usefully) and spaces
But I don't think you can catch both single quotes and double quotes...
* Here's the smart way, 100% credit to steeldriver
Update to take care of non-alphanumeric characters:
Prepare script:
chmod +x script_name
, thenuse it :
./script_name filename
Information:
[[ -e "$1" ]]
: check if the passed file exists.