1

Or anything in shell script to implement the same thing?

I was doing an assignment that requires us to write a Bourne shell script that shows the last argument of a bunch, e.g.:

lastarg arg1 arg2 arg3 ..... argN

which would show:

argN

I was not sure if there's any equivalencies to hasNext in Java as it's easy to implement. Sorry if I was rude and unclear.

udjat
  • 479
  • 2
  • 10
  • What have you tried? Have you done any original research? What Google searches have you tried? Have you seen any examples of taking input in shell scripts? Please help us help you by showing effort in your questions to give you better answers that are useful to everyone. http://catb.org/~esr/faqs/smart-questions.html – Brian Oct 05 '12 at 04:59
  • I guess StackOverflow is not a learner-friendly place? – udjat Oct 05 '12 at 05:02
  • There is a shift operator. http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_09_07.html – Jayan Oct 05 '12 at 05:03
  • Thank you Jayan. The link does help a noob... – udjat Oct 05 '12 at 05:04
  • 2
    It absolutely is, it's just that in general, you should make an effort to solve the problem yourself and let us know that you have. Also, having a more detailed question makes it more visible to users that may have the same questions. Including keywords from Google searches you have tried makes it so that this question will show up in those searches that otherwise failed you. There are many reasons, I won't go into details here, but your question should be useful to everyone and be more visible not just specific to you or vague and unlikely to be useful or easily found. I hope that makes sense – Brian Oct 05 '12 at 05:05
  • @ Chuhan Frank : Please improve your question with more specifics and details about what have you done. – Jayan Oct 05 '12 at 05:06
  • @Brian I hope the revised question meets the requirement. :) – udjat Oct 05 '12 at 05:11
  • 1
    +1 It is a _lot_ better, thank you for improving your question :) A loop to iterate through the arguments as suggested in one of the answers below is probably similar to what you want. Take a look at [this question](http://stackoverflow.com/questions/1853946/getting-the-last-argument-passed-to-a-shell-script) for some ideas. The selected answer doesn't require arg shifting. Others are bash-specific, so don't fit your specs, per se, but are still helpful. Also note that `$#` is a way to get the number of arguments passed to the script. Cheers! Oh, and welcome to Stack Overflow! – Brian Oct 05 '12 at 06:05
  • Thank you Brian, it's also very helpful! – udjat Oct 05 '12 at 16:10

3 Answers3

1
   #!/bin/bash
   all=($@)

   # to make things short:
   # you can use what's in a variable as a variable name
   last=$(( $# )) # get number of arguments
   echo ${!last} # use that to get the last argument. notice the !



   # while the number of arguments is not 0
   # put what is in argument $1 into next
   # move all arguments to the left
   # $1=foo $2=bar $4=moo
   # shift
   # $1=bar $2=moo
   while [ $# -ne 0 ]; do
       next=$1
       shift
       echo $next
   done

   # but the problem was the last argument...
   # all=($@): put all arguments into an array
   # ${all[n]}: get argument number n
   # $(( 1+2 )): do math
   # ${#all[@]}: get the count of element in an array

   echo -e "all:\t ${all[@]}"
   echo -e "second:\t ${all[1]}"
   echo -e "fifth:\t ${all[4]}"
   echo -e "# of elements:\t ${#all[@]}"
   echo -e "last element:\t ${all[ (( ${#all[@]} -1 )) ]}"

ok, last edit (omg :p)

$ sh unix-java-hasnext.sh  one two three seventyfour sixtyeight
sixtyeight
one
two
three
seventyfour
sixtyeight
all:     one two three seventyfour sixtyeight
second:  two
fifth:   sixtyeight
# of elements:   5
last element:    sixtyeight
0

POSIX-based shell languages don't implement iterators.

The only things you have are for V in words ; do ... ; done or implementing the loop with while and manual stuff to update and test the loop variable.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
0

This is not place to make wild guesses, still: Bash provide shift operator, for loop and more.

(If this is for argument processing you have getopt library. More info in Using getopts in bash shell script to get long and short command line options )

Community
  • 1
  • 1
Jayan
  • 18,003
  • 15
  • 89
  • 143