Reputation: 155
I have a set of tarballs in a directory. I want to select the "greatest" name of the tarballs (i.e. the first name when sorting in reverse order) and capture the name in a bash script variable. My example files are:
goyard@GoYard-BOXER-6404:~/Code/NodejsServer$ ls
reports-1.0.0.tgz reports-1.1.0.tgz reports-2.tgz
In this case, "reports-2.tgz" is the file I want.
From the bash command line, this works fine:
goyard@GoYard-BOXER-6404:~/Code/NodejsServer$ tarball_path=`compgen -G "report*.tgz" | sort -r | head -n 1`
goyard@GoYard-BOXER-6404:~/Code/NodejsServer$ echo $tarball_path
reports-2.tgz
In a bash shellscript, this same command succeeds:
The script:
# !/bin/bash
tarball_path=`compgen -G "report*.tgz" | sort -r | head -n 1`
echo "tarball_path: ${tarball_path}"
The script output:
goyard@GoYard-BOXER-6404:~/Code/NodejsServer$ ./get_report_name.sh
tarball_path: reports-2.tgz
But when I execute the script with sudo, the compgen built-in command cannot be found:
goyard@GoYard-BOXER-6404:~/Code/NodejsServer$ sudo ./get_report_name.sh
[sudo] password for goyard:
./get_report_name.sh: 1: compgen: not found
tarball_path:
I need to use sudo because I will eventually extract this tarball into the /srv directory on my Ubuntu box.
Note: This does not appear to be an issue of using the bash -e option because the compgen command is built in to bash - not passed in.
Why does sudo make a difference here?
... and how can I correct this?
Upvotes: 0
Views: 647