0

I am trying to do a command, but I need to get a command line argument. Here is my problem:

This is the code:

pl_function(){
 node=$(echo "$1" | awk -F'\.' '{print $2}')
 echo $node
}

I have a case statement:

case "$1" in -pl.*)
 pl_function
  ;;
esac

When I run this, it prints a blank.

Also, I would like to include getopts, in order to not care about the order of the arguments. Let's say I have -location followed by an argument and -grep followed by argument and -def_location followed by no argument.

How can I use getopts in this case?

Ed Morton
  • 188,023
  • 17
  • 78
  • 185
user2864207
  • 333
  • 1
  • 6
  • 12
  • What about using functions with not reserved names? `my_function` can be harmless. – fedorqui Oct 15 '13 at 15:53
  • yeah sorry! its actually called pl_function – user2864207 Oct 15 '13 at 15:59
  • 1
    You have a [useless use of echo](http://partmaps.org/era/unix/award.html#echo) and you fail to properly quote the variable you `echo`. The function could be simply `echo "$1" | awk -F '\.' '{ print $2 }'` but see also @JohnKugelman's answer. – tripleee Oct 15 '13 at 16:10
  • You should probably post a separate question about getopts. One question per question, please. – tripleee Oct 15 '13 at 16:13
  • Even more simply, `pl_function () { node=${1#*.}; echo "${node%.*}"; }` (at least, no external processes are necessary). – chepner Oct 15 '13 at 16:13
  • While it works, that is the most eccentrically formatted `case` statement I've seen in a _long_ time. The long option names won't work with `getopts`; you'll need to use GNU `getopt`. See [Using `getopts` in `bash` shell script to get long and short command-line options](http://stackoverflow.com/questions/402377/using-getopts-in-bash-shell-script-to-get-long-and-short-command-line-options/) – Jonathan Leffler Oct 15 '13 at 16:16
  • thank you for the help with getopt – user2864207 Oct 15 '13 at 16:22

2 Answers2

2

Inside a function $1, $2, et al are the function's arguments. You need to explicitly pass the script's $1 when you call a function.

case "$1" in -pl.*)
 pl_function "$1"
  ;;

Also, you could use a bit of shell script magic to do the string splitting instead of invoking awk:

node=${1#*.}    # `#*.' removes a substring matching `*.' from the
                # front of $1
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
  • pl_function doesn't just print 1 in -pl.1, but it actually uses 1 in an if statement to execute other commands. I need the case statement, just to access the function pl_function when the user enters -pl.1 or -pl.2 etc – user2864207 Oct 15 '13 at 16:03
  • If there are more than two dot separated fields, you also need to do `"${1%%.*}"`. But +1, this is the way to do it. – tripleee Oct 15 '13 at 16:06
  • that actually works too!! but it didnt solve the problem that it printed blank. My mistake was that i didn't pass arguments to the function. – user2864207 Oct 15 '13 at 16:16
1

When i run this, it prints a blank.

It would because you do not pass any parameter to the function that expects one. Replace your function call with

my_function "$1"

Moreover, as pointed out by @fedorqui do not used reserved words for variable/function names.

Community
  • 1
  • 1
devnull
  • 118,548
  • 33
  • 236
  • 227