Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

UNIX question

  • 21-08-2007 07:52PM
    #1
    Closed Accounts Posts: 5,372 ✭✭✭


    I am currently studying for my repeats and I came across a problem.

    the first part was easy enough. take in a variable from the command line and check it was ok.

    but in the second part it asks me to input a series of variables from the command line.

    I know I need to use a loop (while loop is probably the best) but I can't get the coding right in my head. some help would be appreciated.

    I was going along the lines of:
    while [ $i -ne *something* ]
    do
    -code
    

    I don't know how to say 'until the $i string is empty'


Comments

  • Closed Accounts Posts: 12,382 ✭✭✭✭AARRRGH


    It would probably help if you told us what language you are programming in.


  • Closed Accounts Posts: 5,372 ✭✭✭The Bollox


    I dunno what it is, just the unix standard, I didn't think it had a name other than unix

    well Unix shell programming if that makes it any clearer


  • Closed Accounts Posts: 12,382 ✭✭✭✭AARRRGH


    Unix is an Operating System, not a programming language :)

    Do any of these ring a bell?

    Perl
    Bash
    Tcl
    ...?


  • Closed Accounts Posts: 5,372 ✭✭✭The Bollox


    unfortunatly no, shows what we learn in college, eh? :rolleyes:

    it's the this particular one uses the Korn shell

    is there much of a difference between them?


  • Registered Users, Registered Users 2 Posts: 26,449 ✭✭✭✭Creamy Goodness


    you can use the wild card # which counts the amount of command line arguments and stores it in a variable.

    eg ./shellscript.sh 5 6 7 8 ----> $# will contain 4.


  • Advertisement
  • Closed Accounts Posts: 5,372 ✭✭✭The Bollox


    ah right, cheers. unfortunaly I don't have access to UNIX at home so I can't play around with it


  • Registered Users, Registered Users 2 Posts: 26,449 ✭✭✭✭Creamy Goodness


    you can download an UNIX operating system (eg. ubuntu) and use the terminal program in that or download cynwin (emulates a UNIX environment in windows.)

    or if you had a ssh shell in college you could log into that using putty.


  • Closed Accounts Posts: 5,372 ✭✭✭The Bollox


    I use the one in college when I'm there. we have both RedHat and can access a remote desktop thing through Telnet.

    I will deffinitly be getting my hands on Ubuntu once I get myself a laptop. one of the lads in work has it, it's savage. miles ahead of other OS's


  • Closed Accounts Posts: 12,382 ✭✭✭✭AARRRGH


    Can I ask, is it the syntax you are having problems with or the general concept of looping?


  • Registered Users, Registered Users 2 Posts: 6,652 ✭✭✭daymobrew


    The Bollox wrote:
    while [ $i -ne *something* ]
    do
    -code
    
    I don't know how to say 'until the $i string is empty'
    $ man test
    ....
           -n STRING
                  the length of STRING is nonzero
    
           -z STRING
                  the length of STRING is zero
    ....
    
    So you could either test that the string has something or is empty. Put quotes around the variable.
    while [ -n "$i" ]
    do
      echo $i
    done
    


  • Advertisement
  • Subscribers Posts: 4,077 ✭✭✭IRLConor


    #!/bin/sh
    
    for arg in "$@"; do
        echo $arg
    done
    

    does:
    > sh test.sh foo bar 'foo bar' "foo bar"
    foo
    bar
    foo bar
    foo bar
    


  • Closed Accounts Posts: 5,372 ✭✭✭The Bollox


    dublindude wrote:
    Can I ask, is it the syntax you are having problems with or the general concept of looping?
    I know the general idea behind looping. I can perform it in Java and C#, it's just the syntax. like in Java you say
    i = 10
    for (x = 1; x < i; x++)
    {
    blah blah
    x++
    }
    

    I just wanted to know if there was a similar way of doing it in this language


  • Subscribers Posts: 4,077 ✭✭✭IRLConor


    FYI, the language is "sh". On Linux sh is usually just an alias for bash (which is kind of an extended more fully-featured copy of sh). On other UNIX or UNIX-like operating systems sh may not be bash and bash may not exist.

    Type man sh or man bash in a Linux/UNIX terminal or in a Google search box for the language documentation.


  • Closed Accounts Posts: 5,372 ✭✭✭The Bollox


    cool, cheers


  • Registered Users, Registered Users 2 Posts: 23,202 ✭✭✭✭Tom Dunne


    echo $SHELL might be a start, at least you will know what shell you are using.


Advertisement