简体   繁体   English

Perl Bash 脚本中的一行

[英]Perl one liner in Bash script

I have a bash script that runs, and I'm trying to use a Perl one-liner to replace some text in a file variables.php我有一个运行的 bash 脚本,我正在尝试使用 Perl 单行代码替换文件变量中的一些文本。php

However, I would like to check if the Perl one-liner runs successfully and that's where I get hung up.但是,我想检查 Perl 单线是否成功运行,这就是我挂断电话的地方。 I could just output the one-liner and it would work fine, but I would like to know for sure that it ran.我可以只使用 output 单线,它可以正常工作,但我想确定它是否运行。

Basically, the function replace_variables() is the function that does the update, and it's the if statement there that I would like to check if my one-liner worked properly.基本上, function replace_variables() 是进行更新的 function ,我想检查我的单行是否正常工作的 if 语句。

I've tried using the run_command function in that if statement, but that did not work, and I've tried putting the one-liner directly there, which also didn't work.我尝试在 if 语句中使用 run_command function ,但这不起作用,我尝试将单线直接放在那里,这也不起作用。

If I don't wrap it in an if statement, and just call the one-liner directly, everything works as intended.如果我不将它包装在 if 语句中,而只是直接调用单线,那么一切都会按预期工作。

here's the full file这是完整的文件

#!/bin/bash

export CLI_CWD="$PWD"

site_variables() {
  if [ -f "$CLI_CWD/variables.php" ]; then
    return true
  else
    return false
  fi
}

replace_variables() {
  # perl -pi -e 's/(dbuser)(\s+)=\s.*;$/\1 = Config::get("db")["user"];/; s/(dbpass)(\s+)=\s.*;$/\1 = Config::get("db")["pass"];/; s/(dbname)(\s+)=\s.*;$/\1 = Config::get("db")["database"];/' "$CLI_CWD/variables.php"
  if [run_command ]; then
    echo "Updated variables.php successfully"
  else 
    echo "Did not update variables.php"
  fi
}

run_command() {
  perl -pi -e 's/(dbuser)(\s+)=\s.*;$/\1 = Config::get("db")["user"];/; s/(dbpass)(\s+)=\s.*;$/\1 = Config::get("db")["pass"];/; s/(dbname)(\s+)=\s.*;$/\1 = Config::get("db")["database"];/' "$CLI_CWD/variables.php"
}


if [ site_variables ]; then
  replace_variables
else 
  >&2 echo "Current directory ($(pwd)) is not a project root directory"
  exit 4
fi

here's the function where the if statement fails这是 if 语句失败的 function

replace_variables() {
  # perl -pi -e 's/(dbuser)(\s+)=\s.*;$/\1 = Config::get("db")["user"];/; s/(dbpass)(\s+)=\s.*;$/\1 = Config::get("db")["pass"];/; s/(dbname)(\s+)=\s.*;$/\1 = Config::get("db")["database"];/' "$CLI_CWD/variables.php"
  if [run_command ]; then
    echo "Updated variables.php successfully"
  else 
    echo "Did not update variables.php"
  fi
}

You can see that I commented out the one-liner just before the if statement, it works if I let that run and remove the if/else check.你可以看到我在 if 语句之前注释掉了单行,如果我让它运行并删除 if/else 检查,它就可以工作。

here is the original file snippet before the update这是更新前的原始文件片段

//Load from Settings DB
    $dbuser     = 'username';
    $dbpass     = 'password';
    $dbname     = 'database_name';

here is the file snippet after the update would run这是更新运行后的文件片段

//Load from Settings DB
    $dbuser = Config::get("db")["user"];
    $dbpass = Config::get("db")["pass"];
    $dbname = Config::get("db")["database"];

tl;dr and Solution tl;博士和解决方案

This usage of if with [ ] will not give you the result you expect. if with [ ]的这种用法不会给您预期的结果。
What you're looking for你在找什么

...
    if run_command; then
...

Longer explanation更长的解释

Basics of if if的基础知识

  1. if is a shell feature if是 shell 功能
  2. based on the condition, it executes the body contained in between then and fi根据条件,它执行包含在thenfi之间的主体
  3. the "condition" that if checks is a command if检查的“条件”是一个命令
  4. commands usually have a return/exit code.命令通常有一个返回/退出代码。 typically通常
    • 0 for success 0成功
    • 1 (common) and everything else for some error 1 (常见)和其他所有错误
      • eg 127 for command not found例如127表示找不到命令
  5. when the return/exit code is 0, the body is executed当返回/退出代码为0时,执行body
  6. otherwise it is skipped;否则跳过; or control is passed to elif or else或将控制权传递给elif else
  7. the syntax is if <command>; then...语法是if <command>; then... if <command>; then...

Where does that [ ] come from? [ ]是从哪里来的?

  1. test is a command that can check file types and compare values test是一个可以检查文件类型和比较值的命令
  2. [... ] is a synonym for test [... ]test的同义词
    • NB the brackets should be surrounded by spaces on both sides注意括号应该被两边的空格包围
    • if [ -f "/path/to/$filename" ]; then
      • exception: when terminated by new line or ;例外:当被换行或终止时; space not required不需要空间
  3. test (or [ ] ) evaluates expressions and cannot execute other commands or functions test (或[ ] )计算表达式并且不能执行其他命令或函数
  4. if [ expr ]; then if [ expr ]; then is alternate syntax for if test expr; then if [ expr ]; thenif test expr; then if test expr; then

PS: good practice to "quote" your "$variables" when used with test or [ ] PS:与test[ ]一起使用时“引用”您的“$variables”的好习惯

PPS: [[... ]] is a different thing altogether. PPS: [[... ]]完全不同。 not POSIX;不是POSIX; available only in some shells.仅在某些 shell 中可用。 take a look at this thread on the UNIX Stack Exchange看看UNIX 上的这个线程

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM