如何将 shell 脚本移植到 Perl? [英] How do I port a shell script to Perl?

查看:48
本文介绍了如何将 shell 脚本移植到 Perl?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个shell脚本,我如何在Perl中完成同样的事情?

This is a shell script , How do I accomplish the same thing in Perl?

prfile=~/sqllib/db2profile

profile()
{
if [ -f $prfile ] && [ "$prfile" != "" ];
then
        . $prfile
else
        read -p "Enter a valid Profile : " prfile
        profile
fi
}
profile

这里它检查配置文件,如果找到它用执行它.$prfile 否则它再次要求用户提供正确的配置文件

Here it checks for the profile file , if found it executes it with . $prfile else it again asks user for the proper profile file

更新

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

my $profile = "$ENV{'HOME'}/sqllib/db2proile";
# 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);

这奏效了.我希望主目录是动态的而不是硬编码的,因为它们因不同的机器而异.我只是想用 Perl 完成我用 shell 取得的成就.

This worked. I want the home directory to be dynamic rather than hardcoded as they differ for different machines. I'm just trying to accomplish with Perl what I have achieved with shell.

推荐答案

如果我理解你的目标,我认为你不能使用 perl 来完成这个,因为 perl 将作为一个子进程运行,它不能改变shell 的环境(它是父进程).也许这对你有用(未经测试,即兴创作)?

If I understand your objectives, I don't think you can use perl to accomplish this because perl will be running as a child process and it can not change the environment of your shell (it's parent process). Maybe this would work for you (untested, off the cuff)?

prfile=~/sqllib/db2profile
if [ -s "$prfile" ] ; then
   . "$prfile"
else
  while true ; do 
    read -p "Enter a valid Profile : " prfile
    if [ -s "$prfile" ] ; then
       . "$prfile"
       break;
    fi
  done
fi

这篇关于如何将 shell 脚本移植到 Perl?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆