Article 6E3TY Getopts script advice using realpath

Getopts script advice using realpath

by
brandnewcoat
from LinuxQuestions.org on (#6E3TY)
Hi, All

Hope this is the most relevant section. I am writing a script and learning bash while doing it and come at a standstill.

Using the script I want, when giving the -d option, to then give the directory as a $d to use in the script. Below is just a test.sh to see if I can get it working. I would like realpath to output as a full directory address so the script understands where to output to:

#! /bin/bash
while getopts :d:f: options; do
case $options in
d) # Will be to make $d desired directory.
d=realpath $OPTARG
;;
f) # Will be to list files for script to run on.
f=$OPTARG
;;
\?) # Wrong option.
echo "Error: Invalid option"
exit;;
esac
done
echo "$d/$f"

At the moment it outputs: from bash test.sh -d ~/Desktop -f file.png

test.sh: line 6: /home/aleroncw/Desktop: Is a directory
/file.png

I'm new to script writing to this extent because I got the main script to work but not this option thing and it behaves differently when using $d than if I just ran it from command line. So far tried basedir and truepath and not got it working yet.

Any advice? Thanks for reading and looking forward to your responses.
External Content
Source RSS or Atom Feed
Feed Location https://feeds.feedburner.com/linuxquestions/latest
Feed Title LinuxQuestions.org
Feed Link https://www.linuxquestions.org/questions/
Reply 0 comments