ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Hello
I have a function in my script, that accepts arguments($1, $2 etc.)
It works fine when i dont give arguments to my script.
But when i give arguments to my script(in example: myscript my_argument), the variable $1 from my function, gets the value from my sciprt argument, and not the argument from my function.
Anybody got an idea how to solve this problem?
Thanx Hylke
Well, according to the manual eg http://www.faqs.org/docs/bashman/bashref_22.html#SEC22, getopts loops through all the args supplied to the fn and each one will be assigned to OPTARG.
If the OPT is s, then it will call the fn, passing the content of $OPTARG as $1 ie as $1 inside the fn. It may or may not be $1 in the main prog.
Does that help?
If not,can you describe your prob in more detail, as it seems that your prog probably works to me...
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789
Rep:
Well, when you call it with "-s my_sourcefile", you also give it two arguments and the first test on the argument count succeeds ...
What you need to do is just to run getopt first, and then only do your specific processing.
#!/bin/bash
function file_check()
{
[ ! -f "$1" ] && echo "${1} Does not exist." && exit 1
}
function usage()
{
clear
cat <<USAGE_TAG
Usage: coag source destination
Other options
-v : Current version
-h: Show this
-s: source file
-o : Output file
Written by Hylke Donker
Report bugs at http://www.morbus.info or hylke[at]linux(dot)net
All Rights Reserved (c) Morbus 2004
USAGE_TAG
}
function compile()
{
echo "Compiling..."
g++ $1 -o $2 -g -L/usr/X11R6/lib -lglut -lGLU -lGL -lXi -lXmu -lX11 -lXext -lm
return $?
}
while getopts vs:o:h opt ; do
case $opt in
h) usage ;;
v) echo "Current version: 0.2 - stable" ;;
s) sourcefile=$OPTARG ;;
o) outputfile=$OPTARG ;;
esac
done
if [ "$#" -eq 2 ] ; then
sourcefile=$1
outputfile=$2
fi
if [ -z "$sourcefile" ] ; then
echo "Type the name of the source file :"
read sourcefile
file_check $sourcefile
fi
if [ -z "$outputfile" ] ; then
echo "Type the name of the output file :"
read outputfile
file_check $outputfile
fi
compile $sourcefile $outputfile
if [ "$#" -eq 2 ] ; then
sourcefile=$1
outputfile=$2
fi
if [ -z "$sourcefile" ] ; then
echo "Type the name of the source file :"
read sourcefile
file_check $sourcefile
fi
if [ -z "$outputfile" ] ; then
echo "Type the name of the output file :"
read outputfile
file_check $outputfile
fi
compile $sourcefile $outputfile
With this:
Code:
[ -n "$1" ] && sourcefile="$1"
[ -n "$2" ] && outputfile="$2"
if [ -z "$sourcefile" ] ; then
echo "Type the name of the source file :"
read sourcefile
fi
file_check $sourcefile
if [ -z "$outputfile" ] ; then
echo "Type the name of the output file :"
read outputfile
fi
file_check $outputfile
compile $sourcefile $outputfile
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.