|
Home > Archive > Unix Programming > June 2007 > simple question
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
|
|
| GUNJAN SHARMA 2007-06-21, 7:23 am |
| while reading a file line by line i want to get the first string of
each line and compare it with the user name, if it matches the
username than i want to perform certain tasks like i want to run a
perl script. I wrote the following commands:
while read line
do
echo $line | { read first rest; echo $first; }
if [ "$first" = "$usrname" ]; then
echo "User name is matched"
exec ./script.pl
exit 0
else
continue
fi
done < $FILE
but this is not working because the scope of $first is inside {} and
as the curley brackets are closed nothing is in $first
when i try to assign this value to some other variable like this:
first_string=$(echo $line | { read first rest; echo $first; } )
it gives syntax error
i even tried something like this:
first_string=$(echo $line | awk '{ print $1 }')
even this is not working
anyone of you can help me out please
| |
|
| GUNJAN SHARMA wrote:
> while reading a file line by line i want to get the first string of
> each line and compare it with the user name, if it matches the
> username than i want to perform certain tasks like i want to run a
> PERL script. I wrote the following commands:
>
>
> while read line
> do
> echo $line | { read first rest; echo $first; }
> if [ "$first" = "$usrname" ]; then
> echo "User name is matched"
> exec ./script.pl
> exit 0
> else
> continue
> fi
> done < $FILE
How about
while read USERNAME REST
do
if [ "$USERNAME" = "$PARAM" ]
...
done < $FILE
| |
| James Carlson 2007-06-21, 1:25 pm |
| GUNJAN SHARMA <er.gunjan@gmail.com> writes:
> while reading a file line by line i want to get the first string of
> each line and compare it with the user name, if it matches the
> username than i want to perform certain tasks like i want to run a
> PERL script. I wrote the following commands:
>
>
> while read line
> do
> echo $line | { read first rest; echo $first; }
> if [ "$first" = "$usrname" ]; then
Several ways to do this:
1. Recast the loop like this:
while read first rest; do
if [ "$first" = "$usrname" ]; then
2. Use shell arguments:
while read line; do
set -- $line
if [ "$1" = "$usrname" ]; then
3. Put the logic into the subexpression:
while read line; do
echo $line | {
read first rest
if [ "$first" = "$usrname" ]; then
4. Use another program (brutally ugly, but ...):
while read line; do
first=`echo $line | awk '{ print $1 }'`
if [ "$first" = "$usrname" ]; then
> but this is not working because the scope of $first is inside {} and
> as the curley brackets are closed nothing is in $first
> when i try to assign this value to some other variable like this:
>
> first_string=$(echo $line | { read first rest; echo $first; } )
>
> it gives syntax error
> i even tried something like this:
>
> first_string=$(echo $line | awk '{ print $1 }')
>
> even this is not working
> anyone of you can help me out please
It might also help to explain what shell and what OS you're using.
--
James Carlson, Solaris Networking <james.d.carlson@sun.com>
Sun Microsystems / 1 Network Drive 71.232W Vox +1 781 442 2084
MS UBUR02-212 / Burlington MA 01803-2757 42.496N Fax +1 781 442 1677
| |
| GUNJAN SHARMA 2007-06-21, 1:25 pm |
| On Jun 21, 5:25 pm, James Carlson <james.d.carl...@sun.com> wrote:
> GUNJAN SHARMA <er.gun...@gmail.com> writes:
>
>
> Several ways to do this:
>
> 1. Recast the loop like this:
>
> while read first rest; do
> if [ "$first" = "$usrname" ]; then
>
> 2. Use shell arguments:
>
> while read line; do
> set -- $line
> if [ "$1" = "$usrname" ]; then
>
> 3. Put the logic into the subexpression:
>
> while read line; do
> echo $line | {
> read first rest
> if [ "$first" = "$usrname" ]; then
>
> 4. Use another program (brutally ugly, but ...):
>
> while read line; do
> first=`echo $line | awk '{ print $1 }'`
> if [ "$first" = "$usrname" ]; then
>
>
>
>
>
>
> It might also help to explain what shell and what OS you're using.
>
> --
> James Carlson, Solaris Networking <james.d.carl...@sun.com>
> Sun Microsystems / 1 Network Drive 71.232W Vox +1 781 442 2084
> MS UBUR02-212 / Burlington MA 01803-2757 42.496N Fax +1 781 442 1677
I have written the script like this:
#!/bin/sh
#
# keep all of the arguments
args=$*
# This contains the username who invoked the script
usrname=`whoami`
# Hardcoding the file to be read
FILE=./file.txt
# Checking whether the file exits and is readable
if [ ! -f $FILE ]; then
echo "The given file $FILE does not exists"
exit 1
elif [ ! -r $FILE ]; then
echo "The given file $FILE is not readable"
exit 1
fi
# read $FILE
while read USERNAME REST
do
if [ "$USERNAME" = "#" ]; then
continue
elif [ "$USERNAME" = " " ]; then
continue
else [ "$USERNAME" = "$usrname" ]
echo "User name is matched"
script.pl $args
exit 0
fi
done < $FILE
# exit normally if the file does not contain the username
echo "Undefined User: $usrname\n"
exit 0
but when i use "exit 0" for the first time it should come out of the
loop and the script but still its going to the next statement
and the last statement is printed
"Undefined User: "
which should not be printed if the username is matched
Can you please help me???
| |
| James Carlson 2007-06-21, 1:25 pm |
| GUNJAN SHARMA <er.gunjan@gmail.com> writes:
> On Jun 21, 5:25 pm, James Carlson <james.d.carl...@sun.com> wrote:
Any news on that front?
[vbcol=seagreen]
> I have written the script like this:
>
> #!/bin/sh
For what it's worth, /bin/sh may be any of several things. On Linux,
it tends to be bash. On Solaris, it's old Bourne shell. On other
systems, (such as AIX) it might be ksh.
> while read USERNAME REST
> do
[...]
> exit 0
> fi
> done < $FILE
As the documentation for sh(1) on Solaris says:
If the input or the output of a while or until loop is
redirected, the commands in the loop are run in a sub-shell,
In other words, that 'exit' statement actually exits from the
subshell, which is why what you're trying to do here doesn't work as
you expect. There are probably many ways to fix this. One might be:
(
while read USERNAME REST
do
if [ "$USERNAME" = "#" ]; then
continue
elif [ "$USERNAME" = " " ]; then
continue
else [ "$USERNAME" = "$usrname" ]
echo "User name is matched"
script.pl $args
exit 0
fi
done
# exit normally if the file does not contain the username
echo "Undefined User: $usrname\n"
exit 0
) < $FILE
Shell programming is pretty subtle business.
--
James Carlson, Solaris Networking <james.d.carlson@sun.com>
Sun Microsystems / 1 Network Drive 71.232W Vox +1 781 442 2084
MS UBUR02-212 / Burlington MA 01803-2757 42.496N Fax +1 781 442 1677
| |
| GUNJAN SHARMA 2007-06-21, 1:25 pm |
| On Jun 21, 6:09 pm, James Carlson <james.d.carl...@sun.com> wrote:
> GUNJAN SHARMA <er.gun...@gmail.com> writes:
>
> Any news on that front?
>
>
>
> For what it's worth, /bin/sh may be any of several things. On Linux,
> it tends to be bash. On Solaris, it's old Bourne shell. On other
> systems, (such as AIX) it might be ksh.
>
>
>
> [...]
>
> As the documentation for sh(1) on Solaris says:
>
> If the input or the output of a while or until loop is
> redirected, the commands in the loop are run in a sub-shell,
>
> In other words, that 'exit' statement actually exits from the
> subshell, which is why what you're trying to do here doesn't work as
> you expect. There are probably many ways to fix this. One might be:
>
> (
> while read USERNAME REST
> do
> if [ "$USERNAME" = "#" ]; then
> continue
> elif [ "$USERNAME" = " " ]; then
> continue
> else [ "$USERNAME" = "$usrname" ]
> echo "User name is matched"
> script.pl $args
> exit 0
> fi
> done
>
> # exit normally if the file does not contain the username
> echo "Undefined User: $usrname\n"
> exit 0
> ) < $FILE
>
> Shell programming is pretty subtle business.
>
> --
> James Carlson, Solaris Networking <james.d.carl...@sun.com>
> Sun Microsystems / 1 Network Drive 71.232W Vox +1 781 442 2084
> MS UBUR02-212 / Burlington MA 01803-2757 42.496N Fax +1 781 442 1677
If i will do like this then it will never go to the statement
echo "Undefined User: $usrname\n"
even if the username doesnot exists :-(
Can you suggest something else??
Thanks,
Gunjan
| |
| James Carlson 2007-06-21, 7:24 pm |
| GUNJAN SHARMA <er.gunjan@gmail.com> writes:
> If i will do like this then it will never go to the statement
> echo "Undefined User: $usrname\n"
Really? Why do you think that?
> even if the username doesnot exists :-(
> Can you suggest something else??
Choose a better language. Several other scripting languages are
available -- such as PERL and Python -- and they don't have as many
sharp edges as Bourne shell does.
--
James Carlson, Solaris Networking <james.d.carlson@sun.com>
Sun Microsystems / 1 Network Drive 71.232W Vox +1 781 442 2084
MS UBUR02-212 / Burlington MA 01803-2757 42.496N Fax +1 781 442 1677
| |
| Barry Margolin 2007-06-22, 1:25 am |
| In article <1182424550.467241.228230@n15g2000prd.googlegroups.com>,
GUNJAN SHARMA <er.gunjan@gmail.com> wrote:
> while reading a file line by line i want to get the first string of
> each line and compare it with the user name, if it matches the
> username than i want to perform certain tasks like i want to run a
> PERL script. I wrote the following commands:
>
>
> while read line
> do
> echo $line | { read first rest; echo $first; }
> if [ "$first" = "$usrname" ]; then
> echo "User name is matched"
> exec ./script.pl
> exit 0
> else
> continue
> fi
> done < $FILE
>
>
> but this is not working because the scope of $first is inside {} and
> as the curley brackets are closed nothing is in $first
Actually it has nothing to do with the curly brackets. It's because of
the pipeline:
barmar $ echo "foo bar" | read first rest
barmar $ echo $first
barmar $
When you use a pipeline the commands are run in subshells, which don't
affect the original shell.
Curly braces don't create variable scope in shell:
barmar $ { foo=abc; echo $foo ; }
abc
barmar $ echo $foo
abc
--
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***
|
|
|
|
|