ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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;
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.
This is difficult to do in bash, that is getting it into variables that way. Maybe there's an alternate means to get to your end result, if you could explain where the data is eventually going to end up.
In most cases to process this type of input you'll need to read the data, parse it and save it to a temporary file... then do something with the temporary file.
If you can guarantee that the input data is a limited number of lines then reading it into arrays is possible, but this breaks down when the input file can be large without bounds.
for i in $(seq 2 $(( ${#ARRAY[*]} - 1)) )
${#ARRAY[*]} <- This gives you the number of elements in ARRAY.
$(( ${#ARRAY[*]} - 1)) ) <- This gives you the number of elements -1 (since array elements start at 0.)
So ${ARRAY[2]} would be element #3.
So the for loop is count from array element #3 to the last element.
This whole section: for i in $(seq 2 $(( ${#ARRAY[*]} - 1)) )
do
echo -n "'${ARRAY[$i]}' "
done
Can be replaced by one line:
echo "${ARRAY[@]:2}"
Which means everything from the 3rd (remember arrays are 0 based) element to the last.
I also realized that it looks a little intricate, but that's what I came up with immediately from what I wanted at that point:
The 'values' are stored separately in the components of ARRAY, beginning wih ARRAY[2]. So, to put each value in quotes and print it, I need to loop over the indices of ARRAY, beginning with 2.
That's essentially what
Code:
for i in $( seq 2 ${#ARRAY[*]} )
would do. Unfortunately, ${#ARRAY[*]} doesn't give the final index but the total number of values, so that loop would end up printing an additional ''. We need to make one iteration less, and $(( ${#ARRAY[*]} - 1 )) subtracts 1 from the value of ${#ARRAY[*]} . It's as simple as that. One could have used some temporary variables to make it more readable instead of putting everything into a single line, but I'd consider that as obfuscation.
After all, the only point of my previous post was to mention the -a switch of read.
Edit: Ouch, this time I was really slow.
Last edited by spirit receiver; 08-28-2006 at 05:38 AM.
Is it possible to check in the for loop that i will always be more than 2 then only go into for loop otherwise not.
Because what is happening is - when array is of size 2
the for loop is trying to execute for i == 2 nd i == 1
What I want is loop shud execute for values which are more than two.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.