[英]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"];
This usage of if
with [ ]
will not give you the result you expect. if
with [ ]
的这种用法不会给您预期的结果。
What you're looking for你在找什么
...
if run_command; then
...
if
if
的基础知识if
is a shell feature if
是 shell 功能then
and fi
then
和fi
之间的主体if
checks is a command if
检查的“条件”是一个命令0
for success 0
成功1
(common) and everything else for some error 1
(常见)和其他所有错误
127
for command not found127
表示找不到命令elif
or else
elif
else
if <command>; then...
if <command>; then...
if <command>; then...
[ ]
come from? [ ]
是从哪里来的?test
is a command that can check file types and compare values test
是一个可以检查文件类型和比较值的命令
[... ]
is a synonym for test
[... ]
是test
的同义词
if [ -f "/path/to/$filename" ]; then
;
;
space not requiredtest
(or [ ]
) evaluates expressions and cannot execute other commands or functions test
(或[ ]
)计算表达式并且不能执行其他命令或函数if [ expr ]; then
if [ expr ]; then
is alternate syntax for if test expr; then
if [ expr ]; then
是if 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.