03-11-2011
Yes, the problem is not the loop but the pipe; things behind a pipe are executed in a separate subshell and don't modify variables in your original shell. Temporary files are actually a pretty good way to solve it.
This User Gave Thanks to Corona688 For This Post:
10 More Discussions You Might Find Interesting
1. Programming
I've been wondering if I can make mutexes much easier to use in C++ with creative use of a locking class and variable scope, but I'm not sure if things happen in the order I want. Here's pseudocode for something that could use the class:
int someclass::getvalue()
{
int retval;
... (0 Replies)
Discussion started by: Corona688
0 Replies
2. Shell Programming and Scripting
Hi,
I am stuck while developing a shell sub-routine which checks the log file for "success" or "failure". The subroutine reads the log file and checks for key word "success", if found it set the variable (found=1). It returns success or failure based on this variable.
My problem is, I can... (2 Replies)
Discussion started by: cjjoy
2 Replies
3. Shell Programming and Scripting
Hi All,
I am new to unix shell scripting,
in the below script "num" is an input file which contains a series of numbers example :
2
3
5
8
I want to add the above all numbers and want the result finally outside the while loop. it prints the value zero instead of the actual expected... (13 Replies)
Discussion started by: nagnatar
13 Replies
4. Shell Programming and Scripting
Hi,
I want to know about the variable scope in shell script.
How can we use the script argument inside the function?
fn () {
echo $1 ## I want this argument should be the main script argument and not the funtion argument.
}
also are there any local,global types in shell script?
if... (3 Replies)
Discussion started by: shellwell
3 Replies
5. Shell Programming and Scripting
Hi,
I use Korn Shell. Searched Forum and modified the way the file is input to the while loop, but still the variable does not seem to be retaining the final count.
while read name
do
Tmp=`echo $name | awk '{print $9 }'`
Count=`cat $Tmp | wc -l`... (6 Replies)
Discussion started by: justchill
6 Replies
6. Shell Programming and Scripting
Hello! Before you "bash" me with
- Not another post of this kind
Please read on and you will understand my problem...
I am using the below to extract a sum of the diskIO on a Solaris server.
#!/bin/sh
PATH=/usr/bin:/usr/sbin:/sbin; export PATH
TEMP1="/tmp/raw-sar-output.txt$$"... (3 Replies)
Discussion started by: haaru
3 Replies
7. Shell Programming and Scripting
Hi All
Is there is any way to maintain the scope of the variable in unix
Example
x=1
j=1
while
do
..
....
....
while
do
..
..
x=x+1
done
#inner most while loop ends here
done
#outer loop ends here (8 Replies)
Discussion started by: parthmittal2007
8 Replies
8. Shell Programming and Scripting
Hi,
I'm a Delphi developer new to linux, new to this forums and new to BASH programming and got a new task in my work: maintaining an existing set of BASH scripts. First thing I want to do is making the code more reliable as in my opinion it's really bad written. So here's the quest:
I'm... (6 Replies)
Discussion started by: rse
6 Replies
9. Programming
I have to admit that i have not used Perl at all and this is a singular occasion where i have to patch an existing Perl script. I dearly hope i do not have to do it again for the next 15 years and therefore try to avoid having to learn the programming language in earnest.
The OS is AIX 7.1, the... (2 Replies)
Discussion started by: bakunin
2 Replies
10. UNIX for Beginners Questions & Answers
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
popen(3S) popen(3S)
NAME
popen(), pclose() - initiate pipe I/O to/from a process
SYNOPSIS
DESCRIPTION
creates a pipe between the calling program and a command to be executed by the POSIX shell, (see sh-posix(1)).
The arguments to are pointers to null-terminated strings containing, respectively, a shell command line and an I/O mode, either for reading
or for writing.
returns a stream pointer such that one can write to the standard input of the command if the I/O mode is by writing to the file stream; and
one can read from the standard output of the command if the I/O mode is by reading from the file stream.
Because open files are shared, a type command can be used as an input filter and a type command as an output filter. If mode is other than
or the result is undefined.
A stream opened by should be closed by which waits for the associated process to terminate and returns the exit status of the command.
However, if a call caused the termination status to be unavailable to then returns -1 with errno set to This can happen in one of the fol-
lowing conditions:
o If the signal handler for is set to
o If the signal handler or another thread in the same process issues or call.
APPLICATION USAGE
After a stream is associated with a pipe by the stream is byte-oriented (see orientation(5)).
RETURN VALUE
Upon successful completion, returns a pointer to an open stream that can be used to read or write to the pipe. Otherwise, it returns a
NULL pointer if files or processes cannot be created and set errno to indicate the error. The success of the command execution can be
checked by examining the return value of
Upon successful return, returns the termination status of the command language interpreter. Otherwise, returns -1 if stream is not associ-
ated with a command and set errno to indicate the error.
ERRORS
The function fails if:
The maximum number of file descriptors allowed are currently open.
The function fails if:
The status of the child process is not available.
WARNINGS
If the original and processes concurrently read or write a common file, neither should use buffered I/O because the buffering will not work
properly. Problems with an output filter can be forestalled by careful buffer flushing, e.g., with see fclose(3S).
SEE ALSO
pipe(2), wait(2), fclose(3S), fopen(3S), system(3S), orientation(5), thread_safety(5).
STANDARDS CONFORMANCE
popen(3S)