Using bash in a Debian system.
bash --version
GNU bash, version 4.4.12(1)-release (x86_64-pc-linux-gnu)
Copyright (C) 2016 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
I defined an alias inside a function. I use this alias to write to a log file. Since I run this command as non-privileged user, I'm using tee to do this: alias is not found.
I get:
./test.sh
Preparing Environment...
/tmp/support/20200525_074655
./test.sh: line 19: WRITE_LOG: command not found
./test.sh: line 20: ifconfig: command not found
./test.sh: line 20: WRITE_LOG: command not found
test.sh
#!/bin/bash
function prepare_environment() {
echo "Preparing Environment..."
TICKET_ID=$(date '+%Y%m%d_%H%M%S')
LOG_LOCATION=/tmp/support/"${TICKET_ID}"
sudo mkdir -p "${LOG_LOCATION}"
echo "${LOG_LOCATION}"
alias WRITE_LOG='sudo tee -a "${LOG_LOCATION}"/report.log > /dev/null'
}
function write_log() {
sudo tee -a "${LOG_LOCATION}"/report.log > /dev/null
}
function collect_information() {
echo "Current Report time: $(date '+%Y/%m/%d %H:%M.%S')" | WRITE_LOG
ifconfig | WRITE_LOG
}
prepare_environment
echo "Current Report time: $(date '+%Y/%m/%d %H:%M.%S')" | write_log
collect_information
Alternatives:
- I have various commands, instead of doing
sudo command > file.logI though this would be a cleaner way to do it. - I can do
sudo command | write_log
Similar question
sudo, then why not just run the script asroot?