Bourne Shell - Problem with while loop variable scope.
Hello
I am having issues with a script I'm working on developing on a Solaris machine.
The script is intended to find out how many times a particular user (by given userid) has logged into the local system for more than one hour today.
Here is my while loop:
I have declared numLogins before the loop, and the problem is after the loop I try and "echo $numLogins" but numLogins always contains 0. Even when my debug line prints out showing that the user has logged in for at least an hour.
I have determined that I believe this is because the while loop (when used in a pipeline) has a seperate variable scope, but I have tried rewriting my loop like:
I also thought it may be with the way I was incrementing my numLogins variable,
I have tried to use:
and a few other methods I can't quite remember.
Any help on this would be extremely appreciated.
Thanks a lot!
You can't use read behind a chain of pipes because commands behind pipes will be in a seperate subshell.
When your pipe chain's wider than the screen it's time to rethink what you're doing, anyway. What's the exact output from last and what's the output you want from your program?
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)
I have a loop with cases
I am working on Bourne shell
for file in *.${Today}*.csv *.${Today}*.txt\
do
case ${file} in
sun_detail)
do something
;;
sum)
do something
;;
mod)
do something
;;
*)
do something
;; (5 Replies)
Hello,
I am trying to write a shell script that maintains the health of the passwd file. The goal is to check for duplicate usernames, UID's etc. I am able to find and sort out the UID and login names via awk (which I would like to use), but I can't figure out how to save the record field into a... (1 Reply)
for (( i=1; i<=3; i++ )); do
for (( j=1; j<=3; j++ )); do
for (( k=1; k<=3; k++ )); do
echo $i$j$k
done
done
done
Will the above code work on a BOURNE shell?
As far as my understanding is, if I am writing the above code in a file..say lol.sh and then running it through the terminal using... (7 Replies)
hi,
I am trying to assign a value through 'read' and all works well until I have a space in the in putted value, for the life of me I cant figure out how to escape this. :wall:
Any ideas?
#!/bin/sh
ask_question() {
question_text="${1}";
question_answer="";
... (2 Replies)
Hello Everyone....
I am trying to print a number sequence in following format using for loop.
I am using a bourne shell. I tried following for loop condition but it is bash syntax.
for (( i=0; i<=5; i++ ))
It is giving syntax error.
Kindly help with the syntax of "for"... (7 Replies)
hi all,
i'm using the following script,
Status=1
Function_do ()
{
while read line;
do
if ; then
#echo $line
if ; then
Status=0
echo " LINKINK ERROR "
fi
fi
done < ldd.log
}
Function_do (4 Replies)
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)
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)
how to use if-loop in bourne shell with multiple conditions like follows
if
then
commands
fi
it gives me an error
test: ] missing
then i put
if ]
it gives me an error
[[ not found
kindly i need the syntex for the bourne shell (5 Replies)