I m new to shell scripting. I did some research and understand that unix treats while and other loops as new shell and hence the variable loose its value outside of the loop.
I found solution for integer variable but in mycase this is a string variable.
here variable loc is a multiline string.
I wish the get the multiline string in variable totalstr this format: string1,string2,string3,string4
But currently it is blank as it looses scope. Can you let me know what should be done ?
I m new to shell scripting. I did some research and understand that unix treats while and other loops as new shell and hence the variable loose its value outside of the loop.
This is not completely wrong but also not completely right:
"UNIX" is completely indifferent about this topic. This is a property of the shell and hence the behavior you see depends on which shell you use (and how you configure it).
Most of todays shells have a common ancestor, the "Bourne Shell", which indeed shows the behavior you noticed. In the following sample:
the while-do..done-loop is the second part of a pipeline and therefore executed in a subshell. Therefore, whatever value you assign to "var", it will be lost outside the loop because after the loop the subshell is simply closed. Notice that defining it up front like this:
will not help: the variable "var" in the subshell may have the same name as the variable "var" in the script shell but they are not the same at all. The only way you can overcome this is by replacing the pipeline with a redirection:
Today, the real Bourne Shell is rarely used at all (IIRC it is from the beginning of the eighties) but mostly one of its two main successors: Korn Shell (ksh) and the Bourne Again Shell (bash). Both are (almost) completely backwards compatible with the Bourne Shell which means every shell script written for Bourne Shell should run on Korn shell as well as bash.
In ksh this (rather unintuitive, IMHO) behavior of the Bourne Shell was abandoned and therefore the two examples above work absolutely identical.
This will produce "100" as output in ksh and not "1", like in the Bourne Shell.
In bash, however, the original behavior of the Bourne Shell was retained and hence would produce "1" too. But because that was unintuitive even for the bash programmers in bash 4 (? i am not sure about the exact version) the shell-option "lastpipe" was introduced. If you set that with
bash will work exactly like ksh (in this regard). Notice, though, that you might still come across bash versions which will not understand this configuration. You will have to do it either this way (redirection):
or you can do it this way (a so-called "here-document"):
Why use read for a variable? This is exactly what a for loop is for:
If your multiline variable is simple enough, i.e. no embedded whitespace besides newline, leading, and trailing -- you can omit the IFS stuff for the same result.
A correction:
produces 101 in ksh and errors in Bourne shell.
While the following
runs on both,
but might produce 1 in an old Bourne shell because it always runs the while loop in a sub shell.
A later Bourne shell has got a fix, but seeing a redirection it still runs the sub shell.
That means all Bourne shells do not work with the previously suggested <<EOF redirection.
The Posix shell has got it all fixed. And all Posix-compatible shells.
But a pipe really forces a sub shell. Because it is the last part of the pipe. Compare with a simple command
Only the ksh has got the (incompatible) modification to run the last part of the pipe in the current shell.
Back to the initial post - that works only in ksh.
The following is portable to all Posix-compatible shells:
As you can see, a sub shell inherits everything from the current shell, but not vice versa.
Hi All,
I have a requirement where there are 2 files saved on unix directory with names anil_111 and anil_222. I just have to retain the latest file and delete the old file from the directory.
Please help me with the shell script to perform this.
Thanks,
Anil (7 Replies)
There's an input file(input.txt) which has the following details :
CBA
BA
<Please note the second record has a LEADING WHITESPACE which is VALID>
I am using the following code to read the content of the said file line by line:
while read p ; do
echo "$p"
done < input.txt
This is the... (1 Reply)
use strict;
use warnings;
open (my $fhConditions, "<input1.txt"); #open input file1
open (my $fhConditions1, "<input2.txt");#open input file2
open (my $w1, ">output1");
open (my $w2, ">output2");
our $l = 10;#set a length to be searched for match
our $site="AAGCTT";#pattern to be matched... (1 Reply)
Hello.
I'm trying to add multiple numbers with varying length and with leading zeroes in it. However, I'm getting the sum (totalHashAccountNumber) without the leading zeroes in it. How do I retain the leading zeroes?
Please pardon the lengthy code.. I'm getting the hash account number from 2... (2 Replies)
hi all,
Is there any way to retain file path?
echo "Please enter your old filename"
read a
#user input : /blah/blah1
echo "please enter the new filename"
read b
#user input : dumb
mv $a $b
What i would like to do is for the user to enter just the "filename" for the new filename... (2 Replies)
Hi Experts,
I have a 2 GB flat file which have unicode field, some of them are blanks and its size is 4000 character. In the existing system SED command removes the spaces. Because of this field itself....it is taking almost three days to complete the file processing. I removed sed and... (0 Replies)
Hi all,
I wrote a shell script in which one of the if condition i tried to set few variables and exported them...and when i am out of if condition i wish to run one of my script which uses those variables exported.
But i found that when i am out of the if statement those variables... (5 Replies)
gunzip fnam.tar.gz
After this command execution... .gz file no longer exists... and only fnam.tar is present.
Is it possible to retain the tar.gz file after after using the above command
thx in advance. (4 Replies)
Bourne shell
Solaris
I'm trying to set a flag to perform an action only when data is found. So I initialize the flag with:
X=0
Then I read the data:
if ; then
while read a b c
do
X=1
done < ${inputFile}
fi
The problem is that X will be set to 1 inside the while loop but when... (5 Replies)
I have problem like this :
while loop
1 var=some expression
2 if then
3 Do something
4 oldVar=$var
5 fi
6 done
Then, I am facing error at line number 2, mentioning invalid argument. Can anyone please help me for, How to retain value in oldVar? (4 Replies)