Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unbound variable with bash script

Im becoming desperate when debugging my script, I used some constructions recommended to me from my senior collegue and I dont know how to make it work properly.

 #!/bin/bash -x
set -ueo pipefail
exec &>/tmp/dq.log
source ${BASH_SOURCE%/*}/env-prd.sh

times=${2:-1}
sleep=${3:-1}

name="all-dq_hourly"


fs_lock_file="/tmp/mwa/jobs/prd-${name}.lock"

( flock -n 200
    log="/var/log/mwa/prd/$(date +%Y-%m-%d)__${name}.log"
    for i in $(seq 1 $times); do
        if [[ ! -f /tmp/stop ]]; then
        couple commands

      fi
        sleep $sleep
    done

) 200>"$fs_lock_file" | tee -a $log

rm $fs_lock_file

From the execs , I can see there is an issue with unbound variable for the tee -a $log part, couple commands get executed allright. I tried to use backtics in the log path, but to no benefit. I suspect the same issue with fs_lock_file, but I havent fixed the logging first yet.
Can somebody open my eyes and tell me what Im missing? Im not able to make the script logging to path specified.

like image 863
k_mishap Avatar asked Oct 10 '17 10:10

k_mishap


People also ask

What is unbound variable in shell script?

A symbol that has not been given a value by assignment or in a function call is said to be “unbound.”

What is $@ in bash script?

bash [filename] runs the commands saved in a file. $@ refers to all of a shell script's command-line arguments. $1 , $2 , etc., refer to the first command-line argument, the second command-line argument, etc. Place variables in quotes if the values might have spaces in them.

What does ${ 1 mean in bash script?

$1 is the first positional parameter passed to the shell. The general format can be written as ${var#patt} too, where patt is matched (shortest match from start) in $var and deleted. Example: var="first=middle=last" echo "${var#*=}"

What does %% mean in bash?

So as far as I can tell, %% doesn't have any special meaning in a bash function name. It would be just like using XX instead. This is despite the definition of a name in the manpage: name A word consisting only of alphanumeric characters and under- scores, and beginning with an alphabetic character or an under- score.


1 Answers

You are assigning the variable log inside a subshell ( [...] ). That variable is not bound outside that subshell.

In this case it is probalby best to just set log outside the subshell, i.e. move the variable assignment before the subshell block.

Generally in similar cases, you could try replacing the subshell parentheses with curly braces (group command syntax) { [...] }.

Group commands are executed in the current shell. Note that in contrast to subshell syntax, lists must be terminated by newline or semicolon, see Compound Commands in the Lists section of the bash(1) manpage.

And as a general best practice, setting variable names, especially constants at the beginning of a script or function helps avoid this kind of bug.

like image 150
Michael Jaros Avatar answered Sep 23 '22 12:09

Michael Jaros