BASH pass variable to function

This is my first attempt at BASH and I have a little problem. The purpose of this script is to clear the ZFS pools and create a log file.

The code works as intended, with the exception of line 19 below. (LogEntry $ LOG_LINE). I added line 18 for debugging purposes and it works.

Line 18 writes that the β€œpool” tank is β€œhealthy” for the magazine, and line 19 only β€œpool”. Functionally, this line does the same (at least before my eyes), so why should one work and not the other.

I assume that the root case is the quote before the pool name in the output of the zpool status command, and I am working on replacing the char for this, but I would like to understand how these two lines are interpedited differently.

Thanks in advance Declan

#!/bin/bash LOG_FILE=/home/declan/log/zfs_scrub LOG_LAST=/home/declan/log/zfs_scrub_last function LogEntry { echo -e "$(date "+%Y %m %d %T") ; $1" >>$LOG_FILE 2>&1 } cp $LOG_FILE $LOG_LAST rm $LOG_FILE LogEntry "Starting ZFS Scrub" for POOL in $(zpool list -H -o name) ; do LogEntry "Starting Scrub on $POOL" zpool scrub $POOL 2>/dev/null LOG_LINE=$(zpool status -x $POOL 2>&1) echo -e "$(date "+%Y %m %d %T") ; $LOG_LINE" >>$LOG_FILE 2>&1 LogEntry $LOG_LINE LogEntry "Ending Scrub on $POOL" done LogEntry "Ending ZFS Scrub" 
+4
source share
2 answers

You need to add double quotes around $LOG_LINE :

 LogEntry "$LOG_LINE" 

Otherwise, each element marked with a space is interpreted as the highlighted argument of the LogEntry function.

Alternatively, you can change your function and use $* or $@ to refer to all arguments:

 function LogEntry { echo -e "$(date "+%Y %m %d %T") ; $*" >>$LOG_FILE 2>&1 } 
+5
source

Based on what I found out here, I was able to completely remove the LOG_LINE variable. Here is the "final" code.

 #!/bin/bash LOG_FILE=/home/declan/log/zfs_scrub LOG_LAST=/home/declan/log/zfs_scrub_last LogEntry () {echo "$(date "+%Y %m %d %T") ; $1" >>$LOG_FILE 2>&1; } cp $LOG_FILE $LOG_LAST rm $LOG_FILE LogEntry "Starting ZFS Scrub" for POOL in $(zpool list -H -o name) ; do LogEntry "Starting Scrub on $POOL" zpool scrub $POOL 2>/dev/null LogEntry "$(zpool status -x $POOL 2>&1)" LogEntry "Ending Scrub on $POOL" done LogEntry "Ending ZFS Scrub" 
0
source

All Articles