Hi All,

I am new to scripting. I need your help to removing spaces from a string and assign them to different variables.

Iwant to call script with one command line argument which is file name which containes different attributes their type and different values eg

fdxE1ConfigAdminStatus int 1 2
fdxE1LineType int 1 2 3
        .
        .
        .

Now I want script take one line every time from this file then in that line it will go till end and puts value in different attributes eg

attr1 = fdxE1ConfigAdminStatus
attr2 = int
attr3 = 1 and 2 [possibly an array]

then it goes to second line and do the same and so on

I wrote follwing program

if [ "$1" = "" ]; then
 echo "$0 <attr filename >";
 exit;
fi
while read line; do
  status="";
  echo $line;
  echo ${#line};
done

First traversal of loop containes fdxE1ConfigAdminStatus int 1 2
Now I want to remove spaces after fdxE1ConfigAdminStatus, int, 1 and 2 and put them in different attributes.

This is bash script.

Could you all please help to me to find out a solution.

Thanks in advance

Edited 3 Years Ago by Reverend Jim: Fixed formatting

I'm a little bit confused, but for command line arguments, Linux will automatically split them whenever it sees a space. As for splitting a string, you need to write a parser. I'm not going to do your homework for you, but start researching. (Try "Linux shell script parser")

echo "fdxE1ConfigAdminStatus int 1 2" |
while read name type values ; do
     set -A valarray $(echo $values)
    echo $name $type ${valarray[0]} ${valarray[1]}
    echo "There are $((${#valarray
[*]} - 2)) more values for $name"
done
This article has been dead for over six months. Start a new discussion instead.