LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-11-2019, 03:50 PM   #1
vincix
Senior Member
 
Registered: Feb 2011
Distribution: Ubuntu, Centos
Posts: 1,008

Rep: Reputation: 75
variable in bash script has no content


I am only interested in what happens with the bolded variable.
Code:
 log1() {
        local MESSAGE="${@}"
        if [[ "${VERBOSE}" = 'true' ]]
        then
           echo "${MESSAGE}"
       fi
       logger -t luser-demo10.sh "${MESSAGE}"
}
backup_file() {
        local FILE="${1}"
        local BACKUP_FILE="$(basename ${FILE}).$(date +%F-%N)"
        log1 "Backing up ${FILE} to ${BACKUP_FILE}"
        backup_ausfuhren=$(cp -v "${FILE}" "${BACKUP_FILE}" 2&>1) || logger -t luser-demo10.sh "Copy unsuccessful: ${backup_ausfuhren}" && return 1
        cp "${FILE}" "${BACKUP_FILE}"
}
readonly VERBOSE='false' # use of global variable - readonly makes sure that another function doesn't change the variable
log1 'Hello!'
log1 'This is fun!'
backup_file 'craap'
If I debug the script, I get:
Code:
+ readonly VERBOSE=false
+ VERBOSE=false
+ log1 'Hello!'
+ local 'MESSAGE=Hello!'
+ [[ false = \t\r\u\e ]]
+ logger -t luser-demo10.sh 'Hello!'
+ log1 'This is fun!'
+ local 'MESSAGE=This is fun!'
+ [[ false = \t\r\u\e ]]
+ logger -t luser-demo10.sh 'This is fun!'
+ backup_file craap
+ local FILE=craap
++ basename craap
++ date +%F-%N
+ local BACKUP_FILE=craap.2019-07-11-774814593
+ log1 'Backing up craap to craap.2019-07-11-774814593'
+ local 'MESSAGE=Backing up craap to craap.2019-07-11-774814593'
+ [[ false = \t\r\u\e ]]
+ logger -t luser-demo10.sh 'Backing up craap to craap.2019-07-11-774814593'
++ cp -v craap craap.2019-07-11-774814593 2
+ backup_ausfuhren=
+ logger -t luser-demo10.sh 'Copy unsuccessful: '
+ return 1
And I can't understand why that is. Both variables are set, and even if they weren't, I would have expected an output.

If I run a similar command in the cli, it works without any problems:
Code:
backup_ausfuhren=$(cp craap craap.date 2>&1)
echo $backup_ausfuhren
cp: cannot stat ‘craap’: No such file or directory
 
Old 07-11-2019, 04:01 PM   #2
scasey
Senior Member
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.6
Posts: 3,299

Rep: Reputation: 1111Reputation: 1111Reputation: 1111Reputation: 1111Reputation: 1111Reputation: 1111Reputation: 1111Reputation: 1111Reputation: 1111
Try running the entire command, including the OR to logger from the CLI and see what happens.

I think, but haven't tested, that if the cp fails, the code branches to logger, and there's no output from that to put in the variable.

Alternately, don't include the OR in the variable so that both the cp output and the logger happens. Again, I haven't tested that either.
 
Old 07-11-2019, 04:09 PM   #3
vincix
Senior Member
 
Registered: Feb 2011
Distribution: Ubuntu, Centos
Posts: 1,008

Original Poster
Rep: Reputation: 75
Well, the variable is empty before logger is invoked, but it does error out, 'cause otherwise logger would have been executed.

Ok, while trying to explain, I realised that I've placed & before > instead of after yeah...
 
Old 07-11-2019, 10:06 PM   #4
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 1,133

Rep: Reputation: 512Reputation: 512Reputation: 512Reputation: 512Reputation: 512Reputation: 512
Yes, typo in your script: 2&>1
Okay in your command line: 2>&1

Last edited by MadeInGermany; 07-11-2019 at 10:50 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Get bc script variable into a bash script variable? 14hei Programming 6 02-26-2016 07:34 AM
[SOLVED] ksh read variable from text file and populate with content of shell variable WindozBytes Programming 4 09-17-2012 01:48 PM
[SOLVED] bash: how ls content of a variable in bash script porphyry5 Programming 6 07-28-2011 01:10 PM
-bash: HISTSIZE: readonly variable -bash: HISTFILESIZE: readonly variable deathsfriend99 Linux - Newbie 4 12-08-2009 12:51 PM
Find variable name by variable content using a subshell UltramaticOrange Programming 3 11-17-2008 03:51 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:54 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration