简体   繁体   English

在bash中定义变量时出错

[英]Error defining variable in bash

I am writing simple housekeeping script. 我正在编写简单的管家脚本。 this script contains following line of codes.This is a sample code i have extracted from the actual code since it is a big file. 该脚本包含以下代码行。这是我从实际代码中提取的示例代码,因为它是一个大文件。

 #!/bin/bash

 ARCHIVE_PATH=/product/file

 FunctionA(){
    ARCHIVE_USER=user1  # archive storage user name (default)
    ARCHIVE_GROUP=group1    # archive storage user group (default)

    functionB
 }

 functionB() {
    _name_Project="PROJECT1"
    _path_Componet1=/product/company/Componet1/Logs/
    _path_Component2=/product/company/Componet2/Logs/


     ##Component1##
     archive "$(_name_Project)" "$(_path_Componet1)" "filename1" "file.log" 
 }

 archive(){
     _name= $1
     _path=$2
     _filename=$3
     _ignore_filename=$4
     _today=`date + '%Y-%m-%d'`
     _archive=${ARCHIVE_PATH}/${_name}_$(hostname)_$(_today).tar
     if [ -d $_path];then
        echo "it is a directory"
     fi
 }

 FunctionA

When i run the above script , i get the following error 当我运行上述脚本时,出现以下错误

@localhost.localdomain[] $ sh testScript.sh
testScript.sh: line 69: _name_Component1: command not found
testScript.sh: line 69: _path_Component2: command not found
date: extra operand `%Y-%m-%d'
Try `date --help' for more information.
testScript.sh: line 86: _today: command not found
it is a directory

Could someone explain me what am i doing wrong here. 有人可以解释一下我在做什么错。

I see the line: _today= date + '%Y-%m-%d' 我看到以下行:_today = date + '%Y-%m-%d'

One error I spotted was resolved by removing the space between the + and the ' like so: 我发现的一个错误可以通过删除+和'之间的空格来解决,如下所示:

_today= date +'%Y-%m-%d' _today = date +'%Y-%m-%d'

I don't see where the _name_Component1 and _name_Component2 variables are declared so can't help there :) 我看不到_name_Component1和_name_Component2变量在哪里声明,所以在那里无济于事:)

Your variable expansions are incorrect -- you're using $() which is for executing a subshell substitution. 您的变量扩展不正确-您正在使用$()来执行子shell替换。 You want ${} , ie: 您需要${} ,即:

 archive "${_name_Project}" "${_path_Componet1}" "filename1" "file.log" 

As for the date error, no space after the + . 至于date错误,请在+后面没有空格。

几件事...您正在使用$(variable),当它在date命令上应为$ {variable}时,请确保+和格式之间没有空格,并且您的name = $ 1,您不想那里的空间

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

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