如何使用shell脚本命令变量? [重复]

时间:2021-07-08 11:29:06

This question already has an answer here:

这个问题在这里已有答案:

The following command works well:

以下命令运行良好:

psql -h localhost -U user dados -f /etc/bkpdb/04-03-2015.sql&

However, when I use a variable in it:

但是,当我在其中使用变量时:

valor=04-03-2015
psql -h localhost -U user dados -f /etc/bkpdb/$valor.sql&

It does not work.

这是行不通的。

It happens the following error:

它发生以下错误:

.sql: No such file or directory2015

2 个解决方案

#1


Your code works fine, the problem is that your file is saved on Windows where lines end with \r\n instead of Unix \n.

您的代码工作正常,问题是您的文件保存在Windows上,其中行以\ r \ n而不是Unix \ n结尾。

You can tell because of the weird, trailing 2015 in the error message. It got there because the tool intended to write out a message like:

您可以告诉我,因为错误消息中的奇怪,尾随2015。它到了那里因为该工具打算写出如下消息:

psql: failed: /etc/bkpdb/04-03-2015\r.sql: No such file or directory

But due to the carriage return \r, the cursor moves to the start of the column, and starts overwriting the start of the message:

但是由于回车\ r \ n,光标移动到列的开头,并开始覆盖消息的开头:

p̶s̶q̶l̶:̶ ̶f̶a̶i̶l̶e̶d̶:̶ ̶/̶e̶t̶c̶/̶b̶k̶p̶d̶b̶/̶0̶4̶-̶0̶3̶-2015
.sql: No such file or directory

You're therefore left with:

你因此离开了:

.sql: No such file or directory2015

To solve this, just convert your file from Windows/DOS format to Unix, either in your text editor or with dos2unix, fromdos or tr -d '\r'. Here's trouble shooting step #1 from the bash tag wiki:

要解决此问题,只需在文本编辑器中或使用dos2unix,fromdos或tr -d'\ _'将文件从Windows / DOS格式转换为Unix。这是从bash标签wiki中解决第1步的问题:

  1. Check whether your script or data has DOS style end-of-line characters

    检查脚本或数据是否具有DOS样式的行尾字符

    • Use cat -v yourfile or echo "$yourvariable" | cat -v .

      使用cat -v yourfile或echo“$ yourvariable”|猫-v。

      DOS carriage returns will show up as ^M after each line.

      DOS回车将在每行后显示为^ M.

      If you find them, delete them using dos2unix (a.k.a. fromdos) or tr -d '\r'

      如果找到它们,请使用dos2unix(a.k.a。fromdos)或tr -d'\ _'删除它们

#2


Try:

valor=04-03-2015
psql -h localhost -U user dados -f /etc/bkpdb/${valor}.sql &

#1


Your code works fine, the problem is that your file is saved on Windows where lines end with \r\n instead of Unix \n.

您的代码工作正常,问题是您的文件保存在Windows上,其中行以\ r \ n而不是Unix \ n结尾。

You can tell because of the weird, trailing 2015 in the error message. It got there because the tool intended to write out a message like:

您可以告诉我,因为错误消息中的奇怪,尾随2015。它到了那里因为该工具打算写出如下消息:

psql: failed: /etc/bkpdb/04-03-2015\r.sql: No such file or directory

But due to the carriage return \r, the cursor moves to the start of the column, and starts overwriting the start of the message:

但是由于回车\ r \ n,光标移动到列的开头,并开始覆盖消息的开头:

p̶s̶q̶l̶:̶ ̶f̶a̶i̶l̶e̶d̶:̶ ̶/̶e̶t̶c̶/̶b̶k̶p̶d̶b̶/̶0̶4̶-̶0̶3̶-2015
.sql: No such file or directory

You're therefore left with:

你因此离开了:

.sql: No such file or directory2015

To solve this, just convert your file from Windows/DOS format to Unix, either in your text editor or with dos2unix, fromdos or tr -d '\r'. Here's trouble shooting step #1 from the bash tag wiki:

要解决此问题,只需在文本编辑器中或使用dos2unix,fromdos或tr -d'\ _'将文件从Windows / DOS格式转换为Unix。这是从bash标签wiki中解决第1步的问题:

  1. Check whether your script or data has DOS style end-of-line characters

    检查脚本或数据是否具有DOS样式的行尾字符

    • Use cat -v yourfile or echo "$yourvariable" | cat -v .

      使用cat -v yourfile或echo“$ yourvariable”|猫-v。

      DOS carriage returns will show up as ^M after each line.

      DOS回车将在每行后显示为^ M.

      If you find them, delete them using dos2unix (a.k.a. fromdos) or tr -d '\r'

      如果找到它们,请使用dos2unix(a.k.a。fromdos)或tr -d'\ _'删除它们

#2


Try:

valor=04-03-2015
psql -h localhost -U user dados -f /etc/bkpdb/${valor}.sql &