eval in shell scripting


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting eval in shell scripting
# 1  
Old 07-23-2010
eval in shell scripting

I am stuck on something that should really be simple, and was looking for some help..

I am new to shell scripting.Need help on this.....

The script is to find the stale nfs.


Code:
cat file
-     -     -   -  /abcd/1234

I am writing the script to check the nfs errors of above file

Code:
#!/usr/bin/bash
s=eval awk '{ print $1 }' file
t=eval awk '{ print $2 }' file
p=eval awk '{ print $3 }' file

echo $s
echo $t
echo $p
## want to compare the values to "-"

if eval [$s=$t=$p=="-"];
then echo "nfs issue"
else
echo "no"
fi


Not getting the output of $s

how to get the value of $s=-

Thanks in advance
# 2  
Old 07-23-2010
Hi.

You don't need eval for this.

If s, t and p are all equal "-", then

Code:
#!/usr/bin/bash
s=$(awk '{ print $1 }' file)
t=$(awk '{ print $2 }' file)
p=$(awk '{ print $3 }' file)

echo $s
echo $t
echo $p
## want to compare the values to "-"

if [ "$s$t$p" = "---" ];
then
  echo "nfs issue"
else
  echo "no"
fi


You could probably simplify it a bit:

Code:
awk '$1 $2 $3 == "---" { exit 1 }' file

if [ $? -ne 0 ]; then
  echo "nfs issue"
else
  echo "no"
fi

# 3  
Old 07-23-2010
If I understand the problem correctly, a single grep command should be sufficient:

Code:
grep -q '^\(- *\)\{4\}' infile && echo nfs issue

1. If your grep implementation does not understand the above syntax (the curly braces), you'll need a more feature-rich one (/usr/xpg4/bin/grep on Solaris, or GNU grep, for instance). If those are not available, you could use a more sophisticated tool (Perl?).

2. If your grep implementation does not support the -q (quiet) option, you could redirect the output to /dev/null eventually.
# 4  
Old 07-23-2010
All the three replies worked

1.
Code:
sh -x stale1.sh
+ awk '$1 $2 $3 == "---" { exit 1 }' file
+ '[' 1 -ne 0 ']'
+ echo 'nfs issue'
nfs issue


2.
Code:
sh -x stale2.sh
++ awk '{ print $1 }' file
+ s=-
++ awk '{ print $2 }' file
+ t=-
++ awk '{ print $3 }' file
+ p=-
+ echo -
-
+ echo -
-
+ echo -
-
+ '[' --- = --- ']'
+ echo 'nfs issue'
nfs issue

3.
Code:
grep -q '^\(- *\)\{4\}' file && echo nfs issue
nfs issue


Thanks a lot for your quick replies
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Error in eval eval command to print html tags

anyone has any info on why this is complaining??? vivek@vivek-c5e55ef2e ~/TAC $ zoneCounter=1 vivek@vivek-c5e55ef2e ~/TAC $ optUsage1=23% vivek@vivek-c5e55ef2e ~/TAC $ eval eval echo "<th>Zone $zoneCounter </th><th align=\"left\"> \$optUsage$zoneCounter </th>" -bash: syntax error... (13 Replies)
Discussion started by: vivek d r
13 Replies

2. Shell Programming and Scripting

Error in eval eval command to print html tags

anyone has any info on why this is complaining??? vivek@vivek-c5e55ef2e ~/TAC $ zoneCounter=1 vivek@vivek-c5e55ef2e ~/TAC $ optUsage1=23% vivek@vivek-c5e55ef2e ~/TAC $ eval eval echo "<th>Zone $zoneCounter </th><th align=\"left\"> \$optUsage$zoneCounter </th>" -bash: syntax error... (1 Reply)
Discussion started by: vivek d r
1 Replies

3. Shell Programming and Scripting

Strange result of eval, how does eval really work with ssh?

Hi all, some small script with eval turned me to crazy. my OS is linux Linux s10-1310 2.6.16.53-0.8.PTF.434477.3.TDC.0-smp #1 SMP Fri Aug 31 06:07:27 PDT 2007 x86_64 x86_64 x86_64 GNU/Linux below script works well #!/bin/bash eval ssh remotehost date eval ssh remotehost ls below... (1 Reply)
Discussion started by: summer_cherry
1 Replies

4. What is on Your Mind?

Shell Scripting vs Perl scripting

Gents, I have been working in a Solaris/Unix environment for about 9 months. I took some linux classses online before getting the job. But, I am not very good at scripting. I want to learn how to script. Do you think that I should start with Shell scripting or Perl? I wanted to continue with... (2 Replies)
Discussion started by: Pouchie1
2 Replies

5. UNIX for Advanced & Expert Users

Variable assignments specified with eval shell built-in

According to the POSIX specifications eval is a special shell built-in, which should imply that variable assignments specified together with it should remain in effect after the built-in completes. Thus one would expect IFS to be changed after this: var=$'a\nb c' $ IFS=$'\n' eval ' for i in... (4 Replies)
Discussion started by: Scrutinizer
4 Replies

6. Shell Programming and Scripting

Call Shell scripting from Perl Scripting.

Hi How to call a shell scripting through a Perl scripting? Actually I need some value from Shell scripting and passes in the Perl scripting. So how can i do this? (2 Replies)
Discussion started by: anupdas
2 Replies

7. Shell Programming and Scripting

eval in shell scripting

what is the real usage of eval in shell scripting whats the difference between $ cmd && eval cmd (10 Replies)
Discussion started by: mobydick
10 Replies

8. Shell Programming and Scripting

shell - word splitting - using eval

In POSIX shell, we don't have arrays, but we can iterate over a list like this: #!/bin/sh list="Fred Barney Wilma Betty" for i in $list; do echo $i; done Fred Barney Wilma Betty But let's say we want "Mr. Slate" in the list. We know we can't just stick him in there like this:... (5 Replies)
Discussion started by: mjd_tech
5 Replies

9. Shell Programming and Scripting

bash shell: 'exec', 'eval', 'source' - looking for help to understand

Hi, experts. Whould anybody clear explay me difference and usage of these 3 commands (particulary in bash) : exec eval source I've tryed to read the manual pages but did not get much. Also could not get something useful from Google search - just so much and so not exactly, that is... (3 Replies)
Discussion started by: alex_5161
3 Replies

10. Shell Programming and Scripting

difference between AIX shell scripting and Unix shell scripting.

please give the difference between AIX shell scripting and Unix shell scripting. (2 Replies)
Discussion started by: haroonec
2 Replies
Login or Register to Ask a Question