I created a function to log the results of a script and added an argument to the script. You may look at it at https://docs.laswitchtech.com/doku.php?id=documentations:linux:pxelinux
In this script, I added an argument --screen to launch the same script with all the arguments into a screen with the -L switch.
Enable_Screen(){
Check_Package screen
ScreenCMD="./pxelinux.sh"
CMDOptions="$@"
CMDOptions=${CMDOptions// --screen/}
CMD="$ScreenCMD $CMDOptions"
if [ $debug = "true" ]; then
echo -e "${ORANGE}[DEBUG][EXECUTE] screen -S PXE_Linux -L $CMD ${NORM}"
fi
screen -S PXE_Linux -L $CMD
mv screenlog.0 pxelinux.screen.log
exit 0
}
Now I would like to add an option to the argument to append the log.
an example of how I execute the script :
./pxelinux.sh --debug --screen --install-pxelinux
Now this is the example I would like to use
./pxelinux.sh --debug --screen append --install-pxelinux
Since this is an option for the screen function, I do not want it to be forwarded to the screen I am creating. In the screen function, you can see that I remove the --screen from the list of arguments and now I would need to remove append as well if it shows up in the arguments. But only if it's in the options of the --screen argument. Because append is an option to the argument --screen and may or may not be enabled.
Basically, I used this convention for my arguments:
--argument => execute a function in the script
argument => option for the previously stated --argument
Put more simply:
script.sh
#!/bin/bash
Config_Network(){
echo -e "
source /etc/network/interfaces.d/*
# The loopback network interface
auto lo
iface lo inet loopback
# The primary network interface
allow-hotplug eth0
auto eth0
iface eth0 inet static
address $1
netmask $2
gateway $3
" | tee -a /etc/network/interfaces
}
Update_System(){
Command="apt-get update"; Executing "$Command"
Command="apt-get upgrade -y"; Executing "$Command"
}
Restart_System(){
shutdown -r now
}
Check_Package(){
if [ $(dpkg-query -W -f='${Status}' $1 2>/dev/null | grep -c "ok installed") -eq 0 ];
then
Command="apt-get install $1 -y"; Executing "$Command"
fi
}
Executing(){
if [ $debug = "true" ]; then
if eval $1;then
echo -e "${GREEN}[DEBUG ][$(date)][SUCCESS][EXECUTING] $1 ${NORM}" | tee -a $logfile
else
echo -e "${RED}[DEBUG ][$(date)][ERROR ][EXECUTING] $1 ${NORM}" | tee -a $logfile
fi
else
if eval $1;then
echo -e "${GREEN}[DEBUG ][$(date)][SUCCESS][EXECUTING] $1 ${NORM}"
else
echo -e "${RED}[DEBUG ][$(date)][ERROR ][EXECUTING] $1 ${NORM}"
fi
fi
}
while test $# -gt 0
do
case "$1" in
--config-network)
netconf
;;
--update)
Update_System
;;
--restart)
Restart_System
;;
--*)
exit
;;
esac
shift
done
exit 0
now when I execute script.sh, I want to be able to pass $1 $2 $3 to the netconf fonction no matter where it is in the statement.
./script.sh --config-network 10.10.10.10 255.255.255.0 10.10.10.1 --update --restart
./script.sh --restart --config-network 10.10.10.10 255.255.255.0 10.10.10.1 --update
./script.sh --update --restart --config-network 10.10.10.10 255.255.255.0 10.10.10.1 --update