03-04-2008
Doubt??? [scope of variables]
Heres an example.....
[qzv2jm] <~/abc>$ cat textfile
line 1
line 2
line 3
line 4
line 5
[qzv2jm] <~/abc>$ cat try.sh
#/bin/ksh
for runs in 1 2 3
do
A=$runs
echo "Inside A : $A"
done
echo "Outside A : $A" <- works fine
cat textfile | while read var
do
echo $var
x=$var+1
echo "inside x = $x"
done
echo "outside x = $x" <- Does not work
[qzv2jm] <~/abc>$ try.sh
Inside A : 1
Inside A : 2
Inside A : 3
Outside A : 3 <---worked
line 1
inside x = line 1+1
line 2
inside x = line 2+1
line 3
inside x = line 3+1
line 4
inside x = line 4+1
line 5
inside x = line 5+1
outside x = <----doubt??
[qzv2jm] <~/abc>$
Question: How can I make 'x' global so that I get the value outside the loop
.
10 More Discussions You Might Find Interesting
1. Programming
Each thread has a copy of auto variables within a function, but variables
declared as static within a function are common to all threads. To circumvent
this can static variables be placed outside the function. If so, will the
scope of the variable be file only or will it be extern, and will each... (7 Replies)
Discussion started by: sundaresh
7 Replies
2. UNIX for Advanced & Expert Users
Sorry in the wrong forum. Moving this to right forum. (2 Replies)
Discussion started by: Amruta Pitkar
2 Replies
3. Shell Programming and Scripting
My awk script searches for specified patterns in a text file and stores these values into mem variables.
Once this is done I want to Insert these values into a table.
How can I avail of the variable values outside the scope of awk script....
One method that I have tried is to write the... (7 Replies)
Discussion started by: Amruta Pitkar
7 Replies
4. Shell Programming and Scripting
hai franklin, i am rajitha. i have some doubt which am trying to solve from yesturday but am not getting the answer.can you help me out.
the question is as follows:
write a shell scriipt that accepts a string on the command line and greps the file "filename" for that string.
can you suggest me... (1 Reply)
Discussion started by: rajitha7
1 Replies
5. AIX
What is the scope of AIX as I am starting my career as a fresher in AIX administration?? (4 Replies)
Discussion started by: abhishek27
4 Replies
6. Shell Programming and Scripting
I call my script with two parameters
myscript.sh aaa bbb
What is the way to access $1 and $2 values inside a function? I call the function like this
myfuntion $1 $1
but inside of the function, $1 and $2 are empty. Any suggestions? thank you in advanced. (1 Reply)
Discussion started by: aristegui
1 Replies
7. Shell Programming and Scripting
Hi ,
I'm trying to change the variable value in a while loop , however its not working it seems that the problem with subshells while reading the file.
#!/bin/sh
FLAG=0;
cat filename | while read data
do
FLAG=1;
done
echo $FLAG
Should display 1 instead displays 0 (13 Replies)
Discussion started by: dinjo_jo
13 Replies
8. Shell Programming and Scripting
If I set a variable within a while-read loop, sometimes it's local to the loop, sometimes it's global, depending on how the loop is set up. I'm testing this on a Debian Lenny system using both bash and dash with the same results.
For example:
# Pipe command into while-read loop
count=
ls -1... (2 Replies)
Discussion started by: mjd_tech
2 Replies
9. Shell Programming and Scripting
Hi everyone, I am having some problems with my scripts so I hope you could help me.
I am trying to store the result of a division in a variable in tcshell but I have the problem that if:
For example, dividing 2/100 the result is 0.02 but if I store that I have "0".
How can I have 0.02... (8 Replies)
Discussion started by: Bloody
8 Replies
10. Shell Programming and Scripting
Friends,
I am using ksh under SunoS. This is what I have
In file1.sh
NOW=$(date +"%b-%d-%y")
LOGFILE="./log-$NOW.log"
I will be using this file through file1.sh as log file.
I have another script file2.sh which is being called inside my file1.sh. I would like to use the same log... (6 Replies)
Discussion started by: dahlia84
6 Replies
line(1) General Commands Manual line(1)
NAME
line - Reads one line from standard input
SYNOPSIS
line
STANDARDS
Interfaces documented on this reference page conform to industry standards as follows:
line: XCU5.0
Refer to the standards(5) reference page for more information about industry standards and associated tags.
OPTIONS
None
DESCRIPTION
The line command copies one line, up to and including a newline, from standard input and writes it to standard output. Use this command
within a shell command file to read from your terminal. The line command always writes at least a newline character.
NOTES
The line utility has no internationalization features and is marked LEGACY in XCU Issue 5. Use the read utility instead.
EXIT STATUS
Success. End-of-File.
EXAMPLES
To read a line from the keyboard and append it to a file, enter: echo 'Enter comments for the log:' echo ': c' line >>log
This shell procedure displays the message: Enter comments for the log:
It then reads a line of text from the keyboard and adds it to the end of the file log. The echo ': c' command displays a : (colon)
prompt. See the echo command for information about the c escape sequence.
SEE ALSO
Commands: echo(1), ksh(1), read(1), Bourne shell sh(1b), POSIX shell sh(1p)
Functions: read(2)
Standards: standards(5)
line(1)