Cannot execute Unix command in a simple perl script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Cannot execute Unix command in a simple perl script
# 1  
Old 09-23-2011
Question Cannot execute Unix command in a simple perl script

Am trying to lean perl scripting in Unix OS to automate my tasks.

Please find the below perl script i have tried


Code:
#!/usr/bin/perl -w
print "Please Enter the VG name to be checked:";
$A = <>;
print "Please Enter the free size to be checked in GB:";
$B = <>;
        $vgcheck = `vgdisplay -v 2> /dev/null |grep -i "vg name" |grep -i $A`;
        $vgcheckc = `echo "$vgcheck" |wc -c`;
        print "$vgcheckc";
        if ($vgcheckc > 0)
                {
                $FREE = `vgdisplay $A | grep -i 'Free PE' | awk '{print $NF}'`;
                print $FREE;
                $PESIZE = `vgdisplay $A |grep -i 'PE Size' |awk '{print $NF}'`;
                print $PESIZE
                $total = $FREE * $PESIZE;
                $check = $B * 1024;
                        if ($total > $check)
                        {
                        print "Required Free Space is there in VG $A.. Please proceed to extend the Filesystem as requested\n";
                        print "TOtal free Space in VG $A is : $total MB";
                        }
                        else
                        {
                        print "Required Free Space is NOT there in VG $A..New LUNS are needed. CR must be raised \n";
                        print "Total free Space in VG $A is : $total MB\n";
                        $diff = $check - $total;
                        print "$diff MB of lun must be added to vg $A to proceed with Filesystem Extension\n";
                        }
                }
        else
                {
                print "Please give the correct VG name. The given VG is either not present nor It is NOT Activated\n";


I want to get the "free pe" and "pe size" from the vgdisplay command. but the whole vgdisplay command O/P is being stored to the variable

Code:
$FREE = `vgdisplay $A | grep -i 'Free PE' | awk '{print $NF}'`;

O/P of the perl program

Code:
$ perl testperl.pl
Please Enter the VG name to be checked:vgesar
Please Enter the free size to be checked in GB:20
41
Use of uninitialized value in concatenation (.) or string at testperl.pl line 11, <> line 2.
sh[2]: Syntax error at line 2 : `|' is not expected.
--- Volume groups ---
VG Name                     /dev/vgesar
VG Write Access             read/write
VG Status                   available, exclusive
Max LV                      255
Cur LV                      1
Open LV                     1
Max PV                      100
Cur PV                      2
Act PV                      2
Max PE per PV               1599
VGDA                        4
PE Size (Mbytes)            32
Total PE                    3198
Alloc PE                    3129
Free PE                     69
Total PVG                   0
Total Spare PVs             0
Total Spare PVs in use      0

Use of uninitialized value in concatenation (.) or string at testperl.pl line 13, <> line 2.
sh[2]: Syntax error at line 2 : `|' is not expected.
Argument "--- Volume groups ---\nVG Name                     /dev/..." isn't numeric in multiplication (*) at testperl.pl line 14, <> line 2.
Argument "--- Volume groups ---\nVG Name                     /dev/..." isn't numeric in multiplication (*) at testperl.pl line 14, <> line 2.

Moderator's Comments:
Mod Comment Please use [CODE] tags when posting source listings, ...

Last edited by pludi; 09-23-2011 at 03:51 PM..
# 2  
Old 09-23-2011
Please show example output from vgdisplay vgname and vgdisplay -v, I'll be able to help you better.

I suspect $NF's being substituted when it shouldn't be, in a shell they put that in single quotes for a reason, you may need to escape it like \$NF.

When 3/4 of your code is shell code in backticks and the remaining 25% doesn't use any of perl's advantages, I think it's a sign this problem is better solved in shell... You're not even using perl to measure the length of a string, just echoing it into a shell process...
# 3  
Old 09-23-2011
Thanks for your response..
Code:
$ vgdisplay vgesar | grep -i 'Free PE' | awk '{print $NF}'
69

As i said am just starting with perl. this is one block. Am trying to use perl, as i ll have difficulties comparing floating point values in shell script.

I have already tested this with shell script and works fine. but i just want to know why the same unix command gives the same result as executed in a shell script

---------- Post updated at 12:37 AM ---------- Previous update was at 12:35 AM ----------

As requested by you am pasting the vgdisplay $A

$A here is vgesar -> vgdisplay vgesar
Code:
$ vgdisplay vgesar
--- Volume groups ---
VG Name                     /dev/vgesar
VG Write Access             read/write
VG Status                   available, exclusive
Max LV                      255
Cur LV                      1
Open LV                     1
Max PV                      100
Cur PV                      2
Act PV                      2
Max PE per PV               1599
VGDA                        4
PE Size (Mbytes)            32
Total PE                    3198
Alloc PE                    3129
Free PE                     69
Total PVG                   0
Total Spare PVs             0
Total Spare PVs in use      0


Last edited by Scott; 09-23-2011 at 04:22 PM.. Reason: Code tags...
# 4  
Old 09-23-2011
[edit] think we crossposted. I still need the -v one though!

---------- Post updated at 01:12 PM ---------- Previous update was at 01:09 PM ----------

Quote:
I have already tested this with shell script and works fine. but i just want to know why the same unix command gives the same result as executed in a shell script
Because, as already explained, this isn't a shell script, this is shell script inside perl, and things are being substituted inside perl before the shell even gets to it.

You can see the same problem in shell code:

Code:
$ NF=slartibartfast
$ echo '{ $NF }'
{ $NF }
$ echo "'{ $NF }'"
{ slartibartfast }
$

If you wanted the literal string $NF, double quotes won't cut it, even if you single quote things inside double quotes.

I'll work on a solution for you. (still need the -v output)

Last edited by Corona688; 09-23-2011 at 04:17 PM..
# 5  
Old 09-23-2011
Please find attachment of vgdisplay -v

Also just to add $NF comes with awk utility.

to get the last field
# 6  
Old 09-23-2011
Thank you. That helps a lot, because the output I found on google is very different from the output you have.

I know what awk is. You can do the entire thing in awk. It supports floating point.

---------- Post updated at 02:01 PM ---------- Previous update was at 01:34 PM ----------

Code:
#!/bin/sh

printf "Please Enter the VG name to be checked: "
read VOL

printf "Please Enter the free size to be checked in GB: "
read SIZE

vgdisplay "$VOL" 2> /dev/null | awk -v VOL="$VOL" -v SIZE="$SIZE" '
        # Capture the last field of these two lines
        /Free PE/       { FREE=$NF; }
        /PE Size/       { PE=$NF; }
END {   if( (!FREE) || (!PE) )
        {
                print VOL, "does not exist"
                exit
        }

        # Get EXACTLY how many PE is needed.
        NEEDPE=sprintf("%d", ((SIZE*1024)/PE) );
        # .000001 more means we need a whole extra PE.
        if( (SIZE * 1024) - (NEEDPE*PE) > 0) NEEDPE++;

        if(NEEDPE <= FREE)
                printf("%s has enough, %f GB needs %d/%d free PE\n", VOL, SIZE, NEEDPE, FREE);
        else    printf("%s has %d PE, %d more are needed\n", VOL, FREE, NEEDPE-FREE);
}'

# 7  
Old 09-23-2011
Yeah.. thats right.. We can do that in awk too..

But my doubt is simple..

In command line the O/P of the below command is
Code:
$ vgdisplay vgesar | grep -i 'Free PE' | awk '{print $NF}'
69

In shell script to assign this O/P to a variable it is
Code:
A=vgesar
FREE=`vgdisplay vgesar | grep -i 'Free PE' | awk '{print $NF}'`
echo $FREE

How to get the above O/P assigned to a variable in perl script ??

Last edited by Scott; 09-23-2011 at 05:17 PM.. Reason: Code tags
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

PERL: In a perl-scripttTrying to execute another perl-script that SETS SOME VARIABLES !

I have reviewed many examples on-line about running another process (either PERL or shell command or a program), but do not find any usefull for my needs way. (Reviewed and not useful the system(), 'back ticks', exec() and open()) I would like to run another PERL-script from first one, not... (1 Reply)
Discussion started by: alex_5161
1 Replies

2. Linux

How to execute a simple select script using a shell script?

Hi team, I have two select statements and need to run them using SYSDBA user select * from temp_temp_seg_usage; select segment_name, tablespace_name, bytes/ (1024*1024) UsedMb from dba_segments where segment_name='TEMP_TEMP_SEG_USAGE'; Need to run this using a shell script say named... (1 Reply)
Discussion started by: pamsy78
1 Replies

3. Shell Programming and Scripting

Using UNIX command in perl script.

I wish to know if there is any limitation in using unix commands in perl script or it is just we should avoid using them in our perl script. For e.g Below is the command to get the recent file in a dir.: $lcsvFile = `cd "$l_inputfilepath";ls -1t *.CSV|tail -1` Is there any harm in coding... (1 Reply)
Discussion started by: Devesh5683
1 Replies

4. UNIX for Dummies Questions & Answers

Execute a command in different directory through Unix Script

Hi Gurus, I have the below requirement, Execute an unix script which will pick the latest file from the archive directory and do a grep (on multiple patterns) on that file. processingDir="/usr/apps/irdc/informatica/spsf_sales/TgtFiles/ARCHIVE" filename = 'ls Check* | sort -n -k 2 |... (6 Replies)
Discussion started by: diva_thilak
6 Replies

5. Shell Programming and Scripting

execute ssh command via perl

Hi I have a perl command that doesn't seem to be working correctly. It appears to be fine but even when i try and run it manually same thing. Can someone take a look at this and tell me what they think the problem could be? Here is the perl Line: system ("echo 'ssh -t -t $user\@$_ \"cd... (3 Replies)
Discussion started by: vpundit
3 Replies

6. Shell Programming and Scripting

Help making simple perl or bash script to create a simple matrix

Hello all! This is my first post and I'm very new to programming. I would like help creating a simple perl or bash script that I will be using in my work as a junior bioinformatician. Essentially, I would like to take a tab-delimted or .csv text with 3 columns and write them to a "3D" matrix: ... (16 Replies)
Discussion started by: torchij
16 Replies

7. Shell Programming and Scripting

how to execute ksh simple shell script without creating .sh file

please help me to execute a simple shell script like for i in `ls echo $i done . i dont want to create a new sh file to execute it. Can i just type and execute it ? because I always this kind of simple for loops . Please help . Thanks (7 Replies)
Discussion started by: Sooraj_Linux
7 Replies

8. Shell Programming and Scripting

perl/unix: script in command line works but not in perl

so in unix this command works works and shows me a list of directories find . -name \*.xls -exec dirname {} \; | sort -u | > list.txt but when i try running a perl script to run this command my $query = 'find . -name \*.xls -exec dirname {} \; | sort -u | > list.txt';... (2 Replies)
Discussion started by: kpddong
2 Replies

9. Shell Programming and Scripting

Execute in unix not in PERL

Hi All, This below command is working fine with unix box. However i could not able to run it in PERL. kidly suggest??? perl -ne '{push @x, $_}END{pop(@x); print @x}' create2.txt (15 Replies)
Discussion started by: adaleru
15 Replies

10. Shell Programming and Scripting

Net::SSH::Perl->Execute any unix command & display the output in a proper form

Net::SSH::Perl ...... how to print the output in a proper format my $cmd = "ls -l"; my $ssh = Net::SSH::Perl->new($host); $ssh->login($user, $pass); my($stdout, $stderr, $exit) = $ssh->cmd("$cmd"); print $stdout; the script works fine, but i am unable to see the output... (2 Replies)
Discussion started by: gsprasanna
2 Replies
Login or Register to Ask a Question