LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   [bash-scripting]functions + arguments (http://www.linuxquestions.org/questions/programming-9/%5Bbash-scripting%5Dfunctions-arguments-238153/)

hylke 10-03-2004 05:13 AM

[bash-scripting]functions + arguments
 
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

chrism01 10-03-2004 09:41 AM

can you post the script pls

hylke 10-03-2004 11:23 AM

function:
Code:

function file_check()
{


 if [ ! -f "$1" ]; then
  echo "${1} does not exist!"
  exit 1
 fi
}

Piece of code where it goes wrong(from a getopts whileloop):
Code:

s) file_check $OPTARG;;

chrism01 10-03-2004 11:42 AM

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

hylke 10-03-2004 02:02 PM

What is fn and it?

chrism01 10-04-2004 09:15 AM

fn = function
then it will call the function = then the program (ie main section) will call the function

hylke 10-04-2004 10:09 AM

Ok thanx
But i still cant get it working.
My source code:
Code:

#!/bin/bash
function file_check()
{


 if [ ! -f "$1" ]; then
  echo "${1} does not exist!"
  exit 1
 fi
}

if [ "$#" -eq "2" ]; then
 file_check $1
 clear
 echo "Compiling..."
 g++ $1 -o $2 -g -L/usr/X11R6/lib -lglut -lGLU -lGL -lXi -lXmu -lX11 -lXext -lm;

elif [ "$#" -eq "0" ]; then
 echo "Type the name of the source file:"
 read sourcefile
 file_check $sourcefile
 echo "Type the name of the output file:"
 read outputfile
 clear
 echo "Compiling..."
 g++ $sourcefile -o $outputfile -g -L/usr/X11R6/lib -lglut -lGLU -lGL -lXi -lXmu -lX11 -lXext -lm

else
 while getopts vs:o:h opt; do
  case $opt in
    h) clear; echo "Usage: coag source destination"; echo "Other options"; echo "-v : Current version"; echo "-h: Show this"; echo "-s: source file"; echo "-o : Output file"; echo ""; echo "Written by  Hylke Donker"; echo "Report bugs at http://www.morbus.info or hylke[at]linux(dot)net" echo "All Rights Reserved (c) Morbus 2004";;
    v) echo "Current version: 0.2 - stable";;
    s) file_check $OPTARG; echo "Type the name of the output file:"; read outputfile; clear; echo "Compiling..."; g++ $OPTARG -o $outputfile -g -L/usr/X11R6/lib -lglut -lGLU -lGL -lXi -lXmu -lX11 -lXext -lm;;
    o) echo "Type the name of the source file:"; read $sourcefile; file_check $sourcefile; clear; echo "Compiling..."; g++ $sourcefile -o $OPTARG -g -L/usr/X11R6/lib -lglut -lGLU -lGL -lXi -lXmu -lX11 -lXext -lm;;
  esac
 done
fi;


jlliagre 10-04-2004 11:19 AM

Code:

if [ "$#" -eq "2" ]; then
should be
Code:

if [ "$#" -eq "1" ]; then
...

hylke 10-04-2004 12:12 PM

Are you shoure?
Because it needs to arguments(the source and destination-file) to compile it.

jlliagre 10-04-2004 12:22 PM

Well, your description was confusing and showing only one argument
Quote:

But when i give arguments to my script(in example: myscript my_argument)
If I give your script two arguments, it works fine for me.

Can you better describe what you are observing and what you expect ?

hylke 10-04-2004 12:24 PM

yeah, if i give 2 arguments, it works fine too.
But when i type in example:
coag -s my_sourcefile
it checks -s as filename, and not my_sourcefile

jlliagre 10-04-2004 12:33 PM

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.

dustu76 10-05-2004 01:11 AM

Took some liberties with you code:

Code:

#!/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

This should handle all cases.

HTH.

dustu76 10-05-2004 01:37 AM

Seems I did it too fast... Replace this:

Code:

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

HTH.

dustu76 10-05-2004 01:48 AM

I'll be damned :o :o :o . Another update:

Code:

[ -n "$1" ] && sourcefile="$1"
[ -n "$2" ] && outputfile="$2"

should be

Code:

if [ -z "$sourcefile" -o -z "$outputfile" ] ; then
  [ -z "${sourcefile}" -a -n "$1" ] && sourcefile="$1"
  [ -z "${outputfile}" -a -n "$2" ] && outputfile="$2"
fi

If there are any more bugs here, I give up.... :scratch:

[Shouldnt have taken that green drink last night :cry: ]


All times are GMT -5. The time now is 06:40 PM.