Script read variable with for


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script read variable with for
# 1  
Old 01-07-2015
Linux Script read variable with for

Hi All,

How I can read on variable with cycle for in bash script e.g.
Code:
#!/bin/bash

VAR1=`command1 sentence list`
 
for i in $(cat $VAR1); 
do   
   VAR2=`command2 $i`
   echo $VAR2
done

So read VAR1 execute command over this and load in VAR2 then print VAR2,



Thanks you,
Moderator's Comments:
Mod Comment
Please wrap all code, files, input & output/errors in CODE tags as in the forum rules
It makes them easier to read and respects multiple spaces for indenting or fixed width data

Last edited by rbatte1; 01-07-2015 at 01:24 PM.. Reason: Added CODE tags & moderator comment
# 2  
Old 01-07-2015
This is confusing. You're using both back ticks ` and $(). It's not a problem; however, given some syntax issues with your example, it's not clear. For example, what exactly is command1
Code:
VAR1=`command1 sentence list`

This syntax with backtick implies command1 is executable and sentence and list are parameters. As a result when you do the following
Code:
for i in $(cat $VAR1);

that seems to be fundamentally wrong since you are cat'ing the output of what ever is returned (supposedly a file?) by `command1 sentence list`. In addition the syntax of the for statement is wrong. You don't follow a for statement with a semicolon.

Start with explaining what command1 sentence list produces and what command2 produces.
This User Gave Thanks to blackrageous For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to read a two files, line by line in UNIX script and how to assign shell variable to awk ..?

Input are file and file1 file contains store.bal product.bal category.bal admin.bal file1 contains flip.store.bal ::FFFF:BADC:CD28,::FFFF:558E:11C5,6,8,2,1,::FFFF:81C8:CA8B,::FFFF:BADC:CD28,1,0,0,0,::FFFF:81C8:11C5,2,1,0,0,::FFFF:81DC:3111,1,0,1,0 store.bal.... (2 Replies)
Discussion started by: veeruasu
2 Replies

2. Shell Programming and Scripting

How to read * in a variable in shell script??

hi, i have a text file which conatins some fields delimited by space. some fields contains * as entries. cron_file.txt 0 * * * * 0 3 * * * i want to read each line 1 by 1 and store each field in seperate variables n a shell script. i am unable to read the field that contains a *. how... (3 Replies)
Discussion started by: Little
3 Replies

3. Shell Programming and Scripting

Passing the value of variable which is read from command line in called script

Hi, I am calling a Perl script in my shell script. When Perl script is executed it asks for a answer to be entered by user from terminal. How can i pass that value from my shell script ?? I know I can change perl script to default the answer but i dont have access to do that so only option i... (5 Replies)
Discussion started by: varun22486
5 Replies

4. Shell Programming and Scripting

How to read file and load data into a script as variable

I need to read a text file that contain columns of data, i need to read 1st column as a function to call, and others are the data i need to get into a ksh script. I am quite new to ksh scripting, i am not very sure how to read each row line by line and the data in each columns of that line, set... (3 Replies)
Discussion started by: gavin_L
3 Replies

5. Shell Programming and Scripting

How to write script read file and assign it as variable?

Hi all, I want write a csh script which must be able: 1.read a file 2.assign value in file as variable and can i use read in csh script? thx (2 Replies)
Discussion started by: proghack
2 Replies

6. UNIX for Dummies Questions & Answers

to read variable from child script

Hi I have two shell scripts A and B A calls B I would like to use a variable in A the value of which is assigned by B after calling B Thanks in advance Suresh (8 Replies)
Discussion started by: ssuresh1999
8 Replies

7. Shell Programming and Scripting

Can I use read to read content of a variable

Can I use the read command to read the contents of a variable? I'm trying by using the following code and getting nothing back. I'm in a Linux environment. #!/bin/ksh IFS=~ VAR1=1~2~3~4 echo $VAR1 | read a b c d print "$a $b $c $d" (9 Replies)
Discussion started by: nmalencia
9 Replies

8. Shell Programming and Scripting

Perl script variable to read shell command

Solaris 10 Korn shell ksh, Hi there, I have figured out to get yesterday's date which is using the below command: TZ=GMT+24; date +%d-%b-%Y to get the format of 30-Sep-2008 and TZ=GMT+24; date +%Y%m%d to get the format of 20080930. I need this two format. In my perl script below I need... (4 Replies)
Discussion started by: bulkbiz
4 Replies

9. Shell Programming and Scripting

Shell script to read file into variable

the script i am trying to write will allow my server to give itself an ip address. So far i am up to the following but i'm stuck. tracert -m 1 > traceroute.txt 1 routername (ipaddr) 2.094 ms 1.789 ms 1.243 ms i want to get ipaddr as a variable and use it to write the ifcfg-eth... (7 Replies)
Discussion started by: aspect_p
7 Replies

10. Shell Programming and Scripting

Read variable from file in a C shell script

Hi, I have a 1-line file which looks like " First second third 4 five". I need to extract the number (here 4) in that line and put it in a variable. I will use the variable later to make few tests in my C shell script. Can somebody help me? (2 Replies)
Discussion started by: haouesse
2 Replies
Login or Register to Ask a Question