Array in Bash!


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Array in Bash!
# 1  
Old 03-09-2012
Array in Bash!

sorry I am new to the bash scripting since I was used to c shell programming. I am writting a script in bash for which I have to create an array in bash. I was able to do parse one variable through command line by doing": Lets say the below code is part of temp.bash and is called like:
temp.bash -n x -m z
Code:
 
while [ $1 ]; do
  if [ $1 == "-n" ]; then
     shift
       n=$i
   else [ $1 == "-m" ]
      shift
       name=$1
       shift
      fi
  done
echo $n
echo $name
 
O/P
x
z

I want a code such that is I pass temp.bash -n x y z -m l k z, the ouput should be
x y z
l k z
That is in an array. Currently I can only pass one character. Can anybody help?
Thanks in advance
# 2  
Old 03-09-2012
A string with spaces in it is not the same thing as an array.

I don't think your if-statement if [ $1 ] does precisely what you think it does.

I'd take a slightly different approach:

Code:
WHERE=""
# $# is the number of arguments.
# While the number of arguments is greater than 0, keep looping.
while [ "$#" -gt 0 ]
do
        case "$1" in
        -n)    WHERE="n"             ;;
        -m)    WHERE="name"          ;;

        *) # All other arguments

                case "$WHERE" in
                n)        n="$n $1"     ;;
                name)   name="$name $1" ;;
                *)        ;;
                esac

                ;;
        esac

        shift
done

# 3  
Old 03-09-2012
Thanks Corona688. I used the code but I am getting syntax error near unexpected token `"$WHERE"`
case "$WHERE" in
It might be verbose but I thought I will write it again what I used"
Code:
WHERE=""
 while ["$#" -gt 0 ]
 do
         case "$1" in
          -n)  WHERE="n"  ;;
          -m)  WHERE="name" ;;
              case "$WHERE" in
               n)   n="$n $1"
             name) name="$name $1"
              esac
           ;;
           esac
           shift
done
echo $n
echo $name

when I type temp.bash with this code like: temp.bash -n x y -m l k, I am expecting to get result:
x y
l k
instead I get the error message describe above.
# 4  
Old 03-09-2012
You forgot a line:

Code:
        *) # All other arguments

That's not pseudocode. The * accepts all arguments that weren't matched by -n or -m before it.

Also, you left some important spaces out of your while-statement:

Code:
while ["$#" -gt 0 ] # Wrong
while [ "$#" -gt 0 ] # Right

# 5  
Old 03-09-2012
Quote:
Originally Posted by dixits
while ["$#" -gt 0 ]
A space is required (non-optional) between [ and "
Code:
while [ "$#" -gt 0 ]

-----------------------------------------------------------
Corona688 has already answered.

Last edited by balajesuri; 03-09-2012 at 01:00 PM.. Reason: Late post
# 6  
Old 03-09-2012
Thanks Corona668. The while loop of not using space was a typo but the problem was using not using :
Code:
*) # All other arguments

Thanks once again.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash for loop array

Hi there, A bit new to bash and am having an issue with a for loop. I look for filenames in a specified directory and pull the date string from each meeting a certain criteria, and then would like to make a directory for each date found, like this: search 20180101.gz 20180102.gz 20180103.gz... (5 Replies)
Discussion started by: mwheeler12
5 Replies

2. Shell Programming and Scripting

Bash Array connectin to another Array

Hello, i have a script that i need account_number to match a name. for exsample : ACCOUNT_ID=(IatHG8DC7mZbdymSoOr11w KbnlG2j-KRQ0-1_Xk356s8) and i run a loop curl requst with this the issue is that i want to know on which account were talking about so bash will know this : ... (4 Replies)
Discussion started by: batchenr
4 Replies

3. Shell Programming and Scripting

Bash arrays: rebin/interpolate smaller array to large array

hello, i need a bit of help on how to do this effectively in bash without a lot of extra looping or massive switch/case i have a long array of M elements and a short array of N elements, so M > N always. M is not a multiple of N. for case 1, I want to stretch N to fit M arrayHuge H = (... (2 Replies)
Discussion started by: f77hack
2 Replies

4. Shell Programming and Scripting

Array Issue In Bash

Hi, I have the following code which is giving error mentioned below. Please can you support on this. Also suggest how can we access all the items against single vasservicename in circlename array,i.e, vasservicename say MTSTV will be available to all circles which are mentioned in the array... (2 Replies)
Discussion started by: siramitsharma
2 Replies

5. UNIX for Dummies Questions & Answers

Help with bash array

Hi, I would like to remove audio tracks from several video clips contained in a single directory using the command files=(*.mpg); ffmpeg -i "${files}" -vcodec copy -an na/output.mpg I want each processed file outputted to the sub-directory na retaining the original file name, how do I do that?... (4 Replies)
Discussion started by: jeppe83
4 Replies

6. Shell Programming and Scripting

Bash 3.2 - Array / Regex - IF 3rd member in array ends in 5 digits then do somthing...

Trying to do some control flow parsing based on the index postion of an array member. Here is the pseudo code I am trying to write in (preferably in pure bash) where possible. I am thinking regex with do the trick, but need a little help. pesudo code if == ENDSINFIVEINTS ]]; then do... (4 Replies)
Discussion started by: briandanielz
4 Replies

7. Shell Programming and Scripting

Bash array manipulation

seeking assistance on comparing two arrays using bash: array1=(disk1, disk2, disk3, disk5, disk7, vol1, vol2, vol3, vol4, vol5) array2=(disk2, disk5 vol2, vol4 ) 1) if two arrays have same elements; EXIT else populate array3 & array4 with elements that are different between array1 & array2 as:... (3 Replies)
Discussion started by: solaix14
3 Replies

8. OS X (Apple)

Associated Array in bash

I am using code as below to save data in associated array on Mac OS 10.8. #!/bin/sh storyboardExt=".storyboard" stringsExt=".strings" newStringsExt=".strings.new" oldStringsExt=".strings.old" localeDirExt=".lproj" baseLprojDir="Base${localeDirExt}" echo "${baseLprojDir}" # Find... (2 Replies)
Discussion started by: mikezang
2 Replies

9. Shell Programming and Scripting

bash array problem

hi friends., i have two files yy.dat and mm.dat containing 110 elements in each if i read them into variables it is just showing only 4 elements instead of 110 elements My script is like this ################################## /bin/bash declare -a yy=(`cat yy.dat`) echo "No of values in... (1 Reply)
Discussion started by: yagnesh
1 Replies

10. Shell Programming and Scripting

bash array

I'm trying to move files based on a certain day..my question is on array's, what's wrong with the way I'm stepping through the array? what i have gives me syntax errors..any ideas...so far I have #! /bin/bash sun_d=`cal |grep -v |awk '{printf " " $1}' ` echo "$sun_d" ls -l... (1 Reply)
Discussion started by: gubten
1 Replies
Login or Register to Ask a Question