-1

I need to check of the profile file exists , so i wrote this for the test purpose

#!/usr/bin/perl
use strict;
use warnings;

my $prfle=`~/sqllib/db2profile`;
print $prfle;

But its printing nothing ...

the script checks for the profile file and if not found it will ask the user until the valid path is provided and executes that profile file , I implemented this in shell script successfully but coming across trouble in perl

mviswa
  • 147
  • 1
  • 1
  • 10

2 Answers2

4

In Perl, backticks execute a shell command. For example, this would print hi:

`echo hi`;

To check if a file exists, use -e:

$prfle= '~/sqllib/db2profile';
if (-e $prfle) {
    print "File Exists!\n";
}

Note the single quotes ' instead of backticks ` around the string literal.

Andomar
  • 232,371
  • 49
  • 380
  • 404
  • `prfile=~/sqllib/db2profile profile() { if [ -f $prfile ] && [ "$prfile" != "" ]; then . $prfile else read -p "Enter a valid Profile : " prfile profile fi } profile ` **How do i do this in perl** – mviswa Feb 24 '13 at 16:17
  • Looks like @TLP has provided a Perl version of your bash script :) – Andomar Feb 24 '13 at 16:44
2

Based on your comments, I suspect you want something like this:

my $profile = '';                     # default profile
while (not -e $profile) {             # until we find an existing file
    print "Enter a valid profile: "; 
    chomp($profile = <>);             # read a new profile 
}
qx($profile);                         # execute this file

There are more than one option to execute the file. qx() is the same as backticks and will return standard output. system() will return the return value given from the system for the executed command. exec() will execute the command and exit your perl script, effectively ignoring any code following the exec. Based on your needs, select the option which suits you best.

TLP
  • 66,756
  • 10
  • 92
  • 149
  • Hi , You are write but the only thing i need is , generally the profile file lies in **home-directory/sqllib/bin** in a way **~/sqllib/bin** , so if not found there then we ask for a valid profile – mviswa Feb 24 '13 at 16:51
  • Then you enter that where it says "default profile". – TLP Feb 24 '13 at 16:57
  • You are not getting me , that is what i am saying **my $profile = '~/sqllib/db2profile';** is giving me **~/sqllib/db2profile** instead of **/home-dir/sqllib/db2profile** – mviswa Feb 24 '13 at 17:06
  • Are you saying that perl is not recognizing the `~/` syntax? I think that `~/` in the shell is the same as `$HOME`? In which case you can try `$ENV{'HOME'}/sqllib/db2profile`. – TLP Feb 24 '13 at 17:10
  • ill give a try and let you know – mviswa Feb 24 '13 at 17:47
  • @mViswa It's not easy trying to figure out what you mean. :) Entering a default path and asking for a new path is very simple stuff, but it seems you're asking something else, but you're not saying what it is, so it is rather hard to guess. – TLP Feb 24 '13 at 18:13
  • ok fine ill tell you then , The script i have written checks for the profile file in the default location , if not found it will ask for the user where it is , generally it will be in the home-directory/sqllib/db2profile , in shell i have give the variable the value ~/sqllib/db2profile instead of harcoding it , im trying to find equivalent functionality of assignments in perl , because ~(home) in my installation would be one and your's would be other but ~ solves that problem , so in perl? – mviswa Feb 24 '13 at 18:28
  • @mViswa I understand the general gist of what you are trying to do, but you're just saying "its not working", and since I am not a mindreader, I cannot tell you why its not working. If `~/` does not expand into your default home path, I would expect `$ENV{'HOME'}` to be an option in a *nix system. You have not mentioned if that worked or not. – TLP Feb 24 '13 at 18:45
  • i have executed `#!/usr/bin/perl use strict; use warnings; my $profile = '$ENV{'HOME'}/sqllib/db2profile'; # default profile while (not -e $profile) { # until we find an existing file print "Enter a valid profile: "; chomp($profile = <>); # read a new profile } qx(. $profile); ~ ` itgave me `Bad name after HOME' at rest10.pl line 6` – mviswa Feb 24 '13 at 18:51
  • @mViswa Well, you used single quotes, which does not interpolate variables, so that's not going to work... Also, you already had single quotes around `'HOME'`, so it would seem obvious not to use single quotes. Use double quotes instead. `my $profile = "$ENV{'HOME'} ...` – TLP Feb 24 '13 at 19:02
  • it worked , sorry im not even able to write the basic things in perl , its becoming hard for me as im used to shell scripting very much and its not been 1 week i have been trying in perl .Thanks for your patience in answering me – mviswa Feb 24 '13 at 19:06
  • can you suggest me some book or site where i can find these type of scripts or a good start for learning perl? – mviswa Feb 24 '13 at 19:07
  • There are some tutorials at http://perldoc.perl.org/index-tutorials.html, along with other documentation. There is also the `perldoc` command which you can use to look up things quicker. – TLP Feb 24 '13 at 19:20
  • Note that it is the *shell* that expands `~` to `HOME`. Perl will not do that for you, so `-e '~/sqllib/db2profile'` will *not* work. But I would expect `qx{~/sqllib/db2profile}` to be OK as that is being passed to the shell. – Borodin Feb 24 '13 at 23:39