I encountered such a problem when passing a parameter to a script, the function corresponding to the case menu is not executed. The script accepts parameters as input and performs the appropriate actions
#!/bin/bash
usage () {
echo " Usage:
-h, --help #Displaying help
-p, --proc #Working with directory /proc
-c, --cpu #Working with CPU
-m, --memory #Working with memory
-d, --disks #Working with disks
-n, --network #Working with networks
-la, --loadaverage #Displaying the load average on the system
-k, --kill #Sending signals to processes
-o, --output #Saving the results of script to disk"
exit2
}
proc () {
if [ -n "$1" ]
then
if [ -z "$2" ]
then
ls /proc
else
cat /proc/"$2"
fi
fi
}
parsed_arguments=$(getopt -o hp:c:m:d:n:la:k:o: --long help,proc:,cpu:,memory:,disks:,network:,loadaverage:,kill:,output:)
if [[ "${#}" -eq "" ]]
then
usage
fi
eval set -- "$parsed_arguments"
while :
do
case "$1" in
-h | --help) echo " Showing usage!"; usage
;;
-p | --proc) proc
;;
esac
done
f the script does not receive any parameters, then a description of the options should be displayed, but if the script receives the first parameter starting with "-" or "--" as input, then the functions corresponding to the letter or word following the "-" or "--" should be executed. Example
No parameters:
./script.sh
Usage:
-h, --help #Displaying help
-p, --proc #Working with directory /proc
-c, --cpu #Working with CPU
-m, --memory #Working with memory
-d, --disks #Working with disks
-n, --network #Working with networks
-la, --loadaverage #Displaying the load average on the system
-k, --kill #Sending signals to processes
-o, --output #Saving the results of script to disk"
With one parameters:
./script.sh -p
or
./script.sh --proc
The contents of the /proc directory should be displayed
With additional parameters:
]]./script.sh -p cpuinfo
or
./script.sh --proc cpuinfo
The contents of the file passed through the additional parameter should be displayed
A script without arguments is executed, but not with arguments. Can you tell me what could be the reason for the fact that when passing arguments to the script, the corresponding functions are not executed.
proc
function. – Kusalananda Sep 30 '22 at 12:27getopt
– Stéphane Chazelas Sep 30 '22 at 12:30exit2
command – Stéphane Chazelas Sep 30 '22 at 12:30usage
message to stderr – Stéphane Chazelas Sep 30 '22 at 12:30:
after many of the options which according to usage are not meant to take arguments – Stéphane Chazelas Sep 30 '22 at 12:31-la
) – Stéphane Chazelas Sep 30 '22 at 12:33$#
will never be empty. It will be0
. – terdon Sep 30 '22 at 12:55proc
function correctly. It's about usinggetopt
correctly. Unfortunately I can't help you since I am not on a Linux system with the same non-standardgetopt
implementation that you seem to be trying to use. See Stéphanes' various comments. – Kusalananda Sep 30 '22 at 16:03getopt
, @Kusalananda. It rewrites the command line into a standardised form so that options and their parameters are easy to parse (-ab value
would get rewritten as-a -b value
, etc.) Aside from the "did it work" check being better written to test$?
rather than$#
that part's reasonably ok – Chris Davies Sep 30 '22 at 16:48getopt
appears to be just as nonstandard as the enhanced one, at least I don't seegetopt
listed in https://pubs.opengroup.org/onlinepubs/9699919799.2018edition/idx/ig.html – ilkkachu Sep 30 '22 at 17:25getopt(3)
reference is to the C function, which yes, the GNU C library implements. Most of what it does is standard, but glibc also supports optional option-arguments, which aren't. (Then again, e.g. FreeBSD and OpenBSD also support that, so it's not just GNU) – ilkkachu Oct 01 '22 at 08:49