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.
Asked by Maverick
(1 rep)
Sep 30, 2022, 12:24 PM
Last activity: Sep 30, 2022, 05:23 PM
Last activity: Sep 30, 2022, 05:23 PM