Results 1 to 5 of 5

Thread: shell scripting in ubuntu

  1. #1
    Join Date
    May 2011
    Beans
    1

    shell scripting in ubuntu

    I am using Ubuntu 11.04 and have started learning shell scripting.
    Following commands do not work in shell...though have given in books and online tutorial also.

    array_var=(1 2 3 4 5 6) #declaration
    array_var[0]="test1" #declaration
    $ echo ${array_var[0]} #printing single element of array

    But extensive search gives me

    my_array="a b a a c"
    This is working. But now i want to print particular element of it.
    exm. $echo my_array[0]
    should print a

    code :

    #!/bin/bash
    my_array="a b a a c"
    echo $area[0]
    echo ${area[0]}

    error:

    trial1.sh: 4: Bad substitution

    Any help !!!!!
    But as I said please do it for Ubuntu 11.04.
    Is it a Bug ????

  2. #2
    Join Date
    Nov 2008
    Location
    Darmstadt, Germany
    Beans
    Hidden!
    Distro
    Ubuntu 18.04 Bionic Beaver

    Re: shell scripting in ubuntu

    This is not a bug. The commands you are trying to execute are not Bourne shell or Bash shell commands. It looks like PHP or Perl. What is your book called?

    You can use PHP and Perl under Linux. You can use the Software Center to install them.

    EDIT: Also, how come you declare the "array" (it's actually not an array if you use quotes to declare it, it's a string) as "my_array". Then you refer to "area". How can you expect it to work when the variable names aren't even equal?
    Last edited by ysangkok; May 19th, 2011 at 01:33 PM.

  3. #3
    Join Date
    Feb 2008
    Beans
    5,636

    Re: shell scripting in ubuntu

    Code:
    #!/bin/bash
    my_array=(a b a a c)
    echo ${my_array[0]}

  4. #4
    Join Date
    Apr 2005
    Location
    Finland/UK
    Beans
    Hidden!
    Distro
    Ubuntu 16.04 Xenial Xerus

    Re: shell scripting in ubuntu

    Quote Originally Posted by saj1919 View Post
    my_array="a b a a c"
    This is working. But now i want to print particular element of it.
    exm. $echo my_array[0]
    should print a
    You aren't declaring an array here, just a single string variable. to declare an array with values a, b, a, a and c you'd do this instead:
    Code:
    my_array=(a b a a c)
    the echo command fails simply because you are trying to read from an array while the my_array is actually just a single variable.

    Quote Originally Posted by saj1919 View Post
    code :

    #!/bin/bash
    my_array="a b a a c"
    echo $area[0]
    echo ${area[0]}

    error:

    trial1.sh: 4: Bad substitution
    ..and once again, you are declaring a single string variable, not an array. Also check the name of the variable you are trying to read from...

    edit. too slow. That's what you get from trying to format your posts nicely.
    Last edited by mcduck; May 19th, 2011 at 01:46 PM.

  5. #5
    Join Date
    May 2011
    Beans
    Hidden!

    Re: shell scripting in ubuntu

    Others have already pointed out whats wrong with the script, so;
    Quote Originally Posted by saj1919 View Post
    trial1.sh: 4: Bad substitution
    Sounds like its being executed in dash. Simply use the name of the script to run it (you already have a shebang to use bash), not sh trial1.sh - which points to dash
    _

Tags for this Thread

Bookmarks

Posting Permissions

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