简体   繁体   中英

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'

One error I spotted was resolved by removing the space between the + and the ' like so:

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

I don't see where the _name_Component1 and _name_Component2 variables are declared so can't help there :)

Your variable expansions are incorrect -- you're using $() which is for executing a subshell substitution. You want ${} , ie:

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

As for the date error, no space after the + .

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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