【问题标题】:Perl and shell script in redhatRedhat 中的 Perl 和 shell 脚本
【发布时间】:2023-11-07 19:34:02
【问题描述】:

我对 Perl、Linux 和 shell 脚本还很陌生。谁能让我知道这段脚本到底在做什么?我已经通过互联网搜索并找不到答案。

#!/bin/bash
set +xe

if [ x"$Link_A" == "x" ] ; then
echo "Link_A parameter not set, DOESNOT execute FUNCTION"
else
cp -f ~/cached-resources/www.github.com/file1/executables/JenkinsScripts/jenkins_feed.pl ./jenkins_feed.pl
perl jenkins_feed.pl $JENKINS_HOME/feed_config.ini $Link_A
fi

感谢任何帮助。

我在 Perl 中添加了源代码并编辑了 shell 参数名称。 我需要更改或附加或放置在系统路径中以使此 Perl 文件运行。

源码:https://github.com/rebeccaus/perl/blob/master/feed.pl

【问题讨论】:

    标签: linux bash perl jenkins


    【解决方案1】:

    这是另一种解释:

    #!/bin/bash
    
    # enable debug
    set +xe 
    
    # this is an old school way to check for an empty variable    
    # modern shells can use [[ $foo == "" ]] to do this
    if [ x"$Link_A" == "x" ] ; then
    
        # a normal error message for when the variable is not set
        echo "Link_A parameter not set, DOESNOT execute FUNCTION"
    else # the variable has a value so we can use it...
    
        # copy this file to the current directory
        cp -f ~/cached-resources/www.github.com/file1/executables/JenkinsScripts/jenkins_feed.pl ./jenkins_feed.pl
    
        # run the script in perl with arguments that include variable
        # substitution and some fixed text
        perl jenkins_feed.pl $JENKINS_HOME/feed_bts.ini $Link_A
    fi
    

    我还重新格式化了脚本,使条件的内部部分缩进。

    来自 cmets 的额外问题

    1. Link_A 是文件吗?

    Link-A 是一个变量。一个变量可以指向一个文件或目录,但不清楚在这种情况下变量将包含什么。必须检查 perl 脚本才能明确回答这个问题。

    1. feed_bts.ini 是什么意思,如果不存在,我需要显式创建这样的文件吗?

    我猜 perl 脚本需要一个文件名作为它的第一个参数。 shell 脚本提供了这个,它期望feed_bts.ini 文件位于$JENKINS_HOME(另一个变量)目录中。

    1. 是否需要将 jenkins_feed.pl 和 feed_bts.ini 保存在同一目录中才能运行脚本?

    由于cpjenkins_feed.pl 最终与 shell 脚本位于同一目录中。它以~/cached-resources/www.github.com/file1/executables/JenkinsScripts/jenkins_feed.pl 开头。请注意,~ 是脚本运行用户的主目录。

    1. perl 脚本从哪个路径执行

    这可能会使用随您的发行版安装的任何库存 perl。从技术上讲,shell 将查看您的 $PATH 中的所有目录,直到找到它或用完目录。

    perl 脚本分析

    【讨论】:

    • 感谢您的精彩解释。我在这里有几个问题。 1. Link_A 是文件吗? 2. feed_bts.ini 是什么意思,如果不存在,我需要明确创建这样的文件吗? 3. 我需要将jenkins_feed.pl 和feed_bts.ini 放在同一目录下才能运行脚本吗? 4. perl 脚本从哪个路径执行?
    • 非常感谢 :-) @小鸡
    • 如果你可以的话,你能帮我处理 perl 脚本吗,因为我无法以正确的方式放置这些东西。这会很棒:-)
    • 请编辑最初的问题以包含脚本中的源代码以及您对此有任何具体问题。
    • 我已经添加了源代码仓库,请您帮忙
    【解决方案2】:

    请参阅下文以逐行了解脚本的使用 -

    #!/bin/bash #### Used to set the type of script "bash"
    set +xe  #### Used to execute the script in debug mode (set +x) and e is used to receive the input from terminal
    
    if [ x"$Link_A" == "x" ] ; then    ##### condition check if x appending something is equal to x means no value in variable LinK_A
    echo "Link_A parameter not set, DOESNOT execute FUNCTION"   ##### print this line if above condition is true
    else  ###### if above condition is not true do below operation
    cp -f ~/cached-resources/www.github.com/file1/executables/JenkinsScripts/jenkins_feed.pl ./jenkins_feed.pl
    #####above line copy the file jenkis_feed.pl from specified directory to current dir forcefully
    perl jenkins_feed.pl $JENKINS_HOME/feed_bts.ini $Link_A #### execute jenkil program which take two input parameter
    fi
    

    【讨论】:

    • 谢谢@vipin kumar 这里的两个问题是Link_A 的一种文件吗? feed_bts.ini 是什么?那是一个文件,关于内容,它是什么类型的文件?
    最近更新 更多