Find the answer to your Linux question:
Results 1 to 4 of 4
new to linux and I'm trying to write a script where I have a while loop checking each argument but I can't use the shift option or else all those ...
  1. #1
    Just Joined!
    Join Date
    Nov 2010
    Posts
    1

    Noob variable question

    new to linux and I'm trying to write a script where I have a while loop checking each argument but I can't use the shift option or else all those arguments go out the door for later use. So I'm trying to describe in my if statement the different variables. Basically what I'm trying to do is something like this variable:

    $($args)

    where $args = $# and is in a while loop that slowly counts down as it loops. The problem is I'm getting errors and I'm not sure if thats the proper format. The whole code looks like this:

    Code:
    args="$#"
    
    while [ "args != "0"]
    do
      if [ -d "$($args)" ]
      then
          x="0"
       else
          echo -e "usage: bla bla"
      fi
      args=`expr $args - 1`
    done
    Thanks in advance.
    Last edited by jamie1414; 11-20-2010 at 07:09 PM.

  2. #2
    Just Joined! barriehie's Avatar
    Join Date
    Apr 2008
    Posts
    81
    So you want the CLI argument array; maybe something like this?

    Code:
    #!/bin/bash
    ##
    
    declare -i how_many=$#
    args=("$@")                          # CLI Argument Array
    
    while [ $how_many != 0 ]
    do
      if [ -d ${args[$how_many]} ]
      then
          x="0"
    #      echo $how_many
          how_many=$how_many-1
      else
          echo -e "usage: bla bla"
      fi
    #  args=`expr $args - 1`
    done
    exit 0
    Short and simple:
    Code:
    #!/bin/sh
    for i in $@;
    do
    echo $i
    done
    Last edited by barriehie; 11-21-2010 at 09:03 PM.

  3. #3
    Just Joined!
    Join Date
    Nov 2010
    Posts
    5

    is bash the same linux to linux

    Hi,
    Newbie to bash.

    Would a generic bash script work the same on SuSe as it would on Ubuntu?
    Pardon me, if that is a goofy question.

    By generic, I just mean like a 'hello world' and I guess I'm asking if, fundamentally,
    bash works on all Linux flavours.

    Thank you.

  4. #4
    Just Joined! barriehie's Avatar
    Join Date
    Apr 2008
    Posts
    81
    Quote Originally Posted by utne2K View Post
    Hi,
    Newbie to bash.

    Would a generic bash script work the same on SuSe as it would on Ubuntu?
    Pardon me, if that is a goofy question.

    By generic, I just mean like a 'hello world' and I guess I'm asking if, fundamentally,
    bash works on all Linux flavours.

    Thank you.
    bash should work like bash irregardless of the OS it's installed on. Might be version differences. See what they say in the programmers section?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...