2012-10-05 5 views
1

또는 동일한 것을 구현하는 셸 스크립트의 내용은 무엇입니까?Java와의 Unix 쉘 동등성 .hasNext()?

나는 한 무리의 마지막 인수를 표시하는 Bourne 쉘 스크립트, 예를 들면 작성하는 우리를 필요로하는 할당하고 있던

:

lastarg ARG1 ARG2에서 arg3를 .....에서 argN

하는 보여줄 것이다 : 그것을 구현하기 쉽게 자바에 hasNext에 대한 equivalencies이 있다면

에서 argN

을 나는 확실하지 않았다. 내가 무례하고 불명확하면 죄송합니다.

+0

당신은 무엇을 시도? 원래 연구를 해본 적이 있습니까? 어떤 Google 검색을 시도 했습니까? 쉘 스크립트에서 입력을받는 예제를 본 적이 있습니까? 모든 사람들에게 유용한 더 나은 답변을 제공하기 위해 질문에 노력을 기울임으로써 도움을주십시오. http://catb.org/~esr/faqs/smart-questions.html – Brian

+0

StackOverflow는 학습자가 사용하기 쉬운 장소가 아닌 것 같습니까? – udjat

+0

시프트 연산자가 있습니다. http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_09_07.html – Jayan

답변

0

POSIX 기반 셸 언어는 반복자를 구현하지 않습니다.

당신이 가지고있는 것은 for V in words ; do ... ; done이거나 while으로 루프를 구현하고 수동으로 루프 변수를 업데이트하고 테스트하는 것입니다.

1
#!/bin/bash 
    all=([email protected]) 

    # 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=([email protected]): 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)) ]}" 

확인, 마지막 편집

(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