How to get the modified value of variable outside the while loop reading from a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to get the modified value of variable outside the while loop reading from a file
# 8  
Old 07-20-2009
To keep the forums high quality for all users, please take the time to format your posts correctly.

First of all, use Code Tags when you post any code or data samples so others can easily read your code. You can easily do this by highlighting your code and then clicking on the # in the editing menu. (You can also type code tags [code] and [/code] by hand.)

Second, avoid adding color or different fonts and font size to your posts. Selective use of color to highlight a single word or phrase can be useful at times, but using color, in general, makes the forums harder to read, especially bright colors like red.

Third, be careful when you cut-and-paste, edit any odd characters and make sure all links are working property.

Thank You.

The UNIX and Linux Forums
# 9  
Old 07-20-2009
Code:
chmod a+rx  readLine.sh

and execute it.
When you say
sh script, you will use sh, not script 1st line shell, and it looks that your sh is something, not bash or ksh.

If you like to run using bash then call
bash readLine.sh

Two method to run script:
1. add exucute priviledges for script file and 1st line include the interpreter path and excute syntax (sh, bash, bash, ksh, awk, perl, php, ...)
Code:
chmod a+rx somescript

1st line:
Code:
#!/usr/bin/some_interpreter

ex. to run:
./somescript

2. method is to start interpreter and give commands in textfile, commandline syntax is not same for every interpreter, but for shells it's always:
Code:
interpreter inputfile

ex. bash bashscript
# 10  
Old 07-21-2009
Thanks dude.
check this

my shell is bash
Code:
    echo $SHELL
    /bin/bash

so iam using bash shell , and u said i changed the permissions to the file as a+x , still the output not showing the updated variables .
but when i run the script with

"bash readLine.sh"

It is showing the correct output .

correct me if i'm wrong.

---------- Post updated 07-21-09 at 01:04 PM ---------- Previous update was 07-20-09 at 04:57 PM ----------

Thanks Dude

Last edited by babusek; 07-21-2009 at 02:49 AM..
# 11  
Old 07-21-2009
i use bash and your original script works fine. are you sure "input.txt" contains lines? or does it even exists?

Code:
-bash-3.2$ ./test
 i Value =5
 Count Value = 15
-bash-3.2$ cat test
count=0
i=0
while read line
do
i=`expr $i + 1`
count=`expr $count + $i`
done < input.txt

echo " i Value =$i "
echo " Count Value = $count"
-bash-3.2$ ./test
 i Value =5
 Count Value = 15
-bash-3.2$


Last edited by ryandegreat25; 07-21-2009 at 04:47 AM..
# 12  
Old 07-21-2009
Quote:
Originally Posted by ryandegreat25
i use bash and your original script works fine. are you sure "input.txt" contains lines? or does it even exists?

Code:
-bash-3.2$ ./test
 i Value =5
 Count Value = 15
-bash-3.2$ cat test
count=0
i=0
while read line
do
i=`expr $i + 1`
count=`expr $count + $i`
done < input.txt

echo " i Value =$i "
echo " Count Value = $count"
-bash-3.2$ ./test
 i Value =5
 Count Value = 15
-bash-3.2$

Thanks buddy,
ya input.txt has lines , and u r code working fine ..
Thanks a lot guys .. keep the spirit
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Bash Variable scope - while loop while reading from a file

Cope sample1: test.sh i=0 echo " Outside loop i = $i " while do i=$(( $i + 1)) echo "Inside loop i = $i " done echo " Out of loop i is : $i " When run output : Outside loop i = 0 Inside loop i = 1 Inside loop i = 2 Inside loop i = 3 Inside loop i = 4 Inside loop i = 5 Inside... (8 Replies)
Discussion started by: Adarshreddy01
8 Replies

2. Shell Programming and Scripting

Sequential Reading from two file in a loop

Hello All, I have two files with me file1.txt and file2.txt file1.txt has: 333 222 111 file2.txt has ccc bbb aaa ccc is related to 333 only, bbb is related to 222 only and aaa is related to 111 only. I have to get the values from each of the file and pass them in the URL... (3 Replies)
Discussion started by: ankur328
3 Replies

3. Shell Programming and Scripting

Removing \r and \n during reading file through while loop

Hi, I am writing in a file through cat command. This file will contain the path of file along with filename. e.g. /home/user/folder1/folder2/filename.txt There might be very large number of this path in same file like say 140 when I try to run while command: while read -r file do //command... (8 Replies)
Discussion started by: Pulkit Lall
8 Replies

4. Shell Programming and Scripting

While loop reading file with multiple conditions

Hi Am trying to print the PIDs of process in a file and trying to grep any PID from that file I set the if condition as $value != "PID" and $value != "-" Assign that number to a variable Am confused since am using while loop to read the line from file and again if condition to check those... (2 Replies)
Discussion started by: Priya Amaresh
2 Replies

5. Shell Programming and Scripting

Loop is not reading tabs from the file

Hi, I am on HP-UX and K shell. When I am using while/for loop for reading a file. It is working fine but not reading tabs: Suppose, if the line is: ; ;COMP; ; ; ; then loop is reading as ; ;COMP; ;... (5 Replies)
Discussion started by: ezee
5 Replies

6. AIX

How to pause a while loop while reading from a file

Hi, I am building a script to grep for a string in all the files from a folder and display the results. I am reading the files one by one by placing the names in other file using while loop my code is as below while read inp do chk=`grep -c "$str" $pth/$inp` ... (2 Replies)
Discussion started by: sekhar gajjala
2 Replies

7. Shell Programming and Scripting

Reading variable from file variable values

Hi, Here is the output of lpstat. I would like to read value of Queue which is(abxxxxb1)and status that is DOWN in first line. i dont care what is in second line. any one can help me.thanks Queue Dev Status Job Files User PP % Blks Cp Rnk ------- ----- ---------... (5 Replies)
Discussion started by: sagii
5 Replies

8. Shell Programming and Scripting

Not access variable outside loop when a reading a file

I am writing a shell script using the korn shell. It seems that I am only able to use local variables within a while loop that is reading a file. (I can't access a variable outside a previously used while loop.) It's been a while since I wrote shell scripts. Here is a sample cat file.txt... (4 Replies)
Discussion started by: ricardo.ludwig
4 Replies

9. UNIX for Dummies Questions & Answers

reading more than one variable into a for loop

Hi, I have a file (details.txt) with 3 rows of variables ie... name postcode age john D fr25dd 25 mark W ab122aa 22 phil C cd343bb 33 What I want to do is read down the list with a loop and add each field into a one line piece of text... So I have a file (test1) which reads;... (3 Replies)
Discussion started by: starsky
3 Replies

10. UNIX for Dummies Questions & Answers

how to retrieve original contents of a modified file (modified using vi)

Made changes to a file using vi editor and saved those changes now realised that the changes are not required How can I get the previous version of the file.i.e the one which was there on which I had made changes (3 Replies)
Discussion started by: novice100
3 Replies
Login or Register to Ask a Question