LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 10-03-2004, 05:13 AM   #1
hylke
Member
 
Registered: Apr 2004
Location: the Netherlands
Distribution: Ubuntu 7.04
Posts: 329

Rep: Reputation: 30
[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
 
Old 10-03-2004, 09:41 AM   #2
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5
Posts: 16,086

Rep: Reputation: 1994Reputation: 1994Reputation: 1994Reputation: 1994Reputation: 1994Reputation: 1994Reputation: 1994Reputation: 1994Reputation: 1994Reputation: 1994Reputation: 1994
can you post the script pls
 
Old 10-03-2004, 11:23 AM   #3
hylke
Member
 
Registered: Apr 2004
Location: the Netherlands
Distribution: Ubuntu 7.04
Posts: 329

Original Poster
Rep: Reputation: 30
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;;
 
Old 10-03-2004, 11:42 AM   #4
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5
Posts: 16,086

Rep: Reputation: 1994Reputation: 1994Reputation: 1994Reputation: 1994Reputation: 1994Reputation: 1994Reputation: 1994Reputation: 1994Reputation: 1994Reputation: 1994Reputation: 1994
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...

Last edited by chrism01; 10-04-2004 at 09:14 AM.
 
Old 10-03-2004, 02:02 PM   #5
hylke
Member
 
Registered: Apr 2004
Location: the Netherlands
Distribution: Ubuntu 7.04
Posts: 329

Original Poster
Rep: Reputation: 30
What is fn and it?

Last edited by hylke; 10-03-2004 at 02:03 PM.
 
Old 10-04-2004, 09:15 AM   #6
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5
Posts: 16,086

Rep: Reputation: 1994Reputation: 1994Reputation: 1994Reputation: 1994Reputation: 1994Reputation: 1994Reputation: 1994Reputation: 1994Reputation: 1994Reputation: 1994Reputation: 1994
fn = function
then it will call the function = then the program (ie main section) will call the function
 
Old 10-04-2004, 10:09 AM   #7
hylke
Member
 
Registered: Apr 2004
Location: the Netherlands
Distribution: Ubuntu 7.04
Posts: 329

Original Poster
Rep: Reputation: 30
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;
 
Old 10-04-2004, 11:19 AM   #8
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Mint, OL
Posts: 9,459

Rep: Reputation: 352Reputation: 352Reputation: 352Reputation: 352
Code:
if [ "$#" -eq "2" ]; then
should be
Code:
if [ "$#" -eq "1" ]; then
...
 
Old 10-04-2004, 12:12 PM   #9
hylke
Member
 
Registered: Apr 2004
Location: the Netherlands
Distribution: Ubuntu 7.04
Posts: 329

Original Poster
Rep: Reputation: 30
Are you shoure?
Because it needs to arguments(the source and destination-file) to compile it.
 
Old 10-04-2004, 12:22 PM   #10
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Mint, OL
Posts: 9,459

Rep: Reputation: 352Reputation: 352Reputation: 352Reputation: 352
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 ?
 
Old 10-04-2004, 12:24 PM   #11
hylke
Member
 
Registered: Apr 2004
Location: the Netherlands
Distribution: Ubuntu 7.04
Posts: 329

Original Poster
Rep: Reputation: 30
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
 
Old 10-04-2004, 12:33 PM   #12
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Mint, OL
Posts: 9,459

Rep: Reputation: 352Reputation: 352Reputation: 352Reputation: 352
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.
 
Old 10-05-2004, 01:11 AM   #13
dustu76
Member
 
Registered: Sep 2004
Distribution: OpenSuSe
Posts: 153

Rep: Reputation: 30
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.
 
Old 10-05-2004, 01:37 AM   #14
dustu76
Member
 
Registered: Sep 2004
Distribution: OpenSuSe
Posts: 153

Rep: Reputation: 30
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.
 
Old 10-05-2004, 01:48 AM   #15
dustu76
Member
 
Registered: Sep 2004
Distribution: OpenSuSe
Posts: 153

Rep: Reputation: 30
I'll be damned . 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....

[Shouldnt have taken that green drink last night ]
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
BASH Script: variable values referencing for console arguments sadarax Programming 1 11-14-2005 05:23 PM
logname: no login name, -bash: [: too many arguments da_kidd_er Linux - General 1 10-27-2004 02:09 PM
bash arguments stuckinhell Programming 6 08-13-2004 05:10 AM
bash scripting - referring to external arguments into loops linsson Linux - General 2 07-23-2004 12:24 PM
Linked Lists: Pointers sent to functions as arguments do not change their valur Jose Muņiz Programming 3 01-12-2004 07:45 PM


All times are GMT -5. The time now is 07:46 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration