From 86ff32ae6d3cd719933c9c8e7a5340283d882de5 Mon Sep 17 00:00:00 2001 From: Pieds-Nus Date: Wed, 21 Sep 2022 12:38:59 +0000 Subject: [PATCH] swapctl : catch error in parameters (non-integer size) --- swapctl.sh | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/swapctl.sh b/swapctl.sh index 79eed19..0aef69c 100644 --- a/swapctl.sh +++ b/swapctl.sh @@ -1,6 +1,12 @@ #!/bin/bash -sep="\n\t#############\n" +# Warning : this script calls heavily on sudo. +# This is not clean. +# But it is easier in my workflow. +# Please consider removing all "sudo" from this file, +# and calling it with administrative rights +# if you want to use it in a proper way. + usage="$0 -d DIR -n NAME -s SIZE -p PRIO add|remove Add or remove swapfiles to micro-manage disk usage. -d DIR : set DIRectory to place swapfile in. Default: /mnt @@ -26,7 +32,14 @@ while getopts hp:s:n:d: arg ; do h) echo -e "$usage" ; exit 0 ;; d) SWAP_FOLDER="$OPTARG" ;; n) SWAP_FILENAME="$OPTARG" ;; - s) SWAP_SIZE="$OPTARG" ;; + s) + if test "$OPTARG" -gt 0 2> /dev/null ; then + SWAP_SIZE="$OPTARG" + else + echo -e "invalid argument: -s $OPTARG\t (size expected in MB without suffix)" + exit 1 + fi + ;; p) SWAP_PRIO_MAX="$OPTARG" ;; *) echo -e "$usage" ; echo "Invalid option: $arg"; exit 1 ;; esac @@ -34,6 +47,8 @@ done shift $((( $OPTIND - 1 ))) +sep="\n\t#############\n" + if test "$1" = "add" ; then N=$(ls $SWAP_FOLDER/$SWAP_FILENAME* | wc -l) @@ -70,19 +85,19 @@ tmpfile=/tmp/swapctl.fstab if test $1 = "add" ; then cat /etc/fstab > $tmpfile - echo -e "$swapfile\t\t\t\t\t\t\t\t\tnone\t\t\tswap\tsw,pri=$prio\t\t\t0\t\t0" >> $tmpfile + echo -e "$swapfile\t\t\t\t\t\t\t\tnone\t\t\tswap\tsw,pri=$prio\t\t\t0\t\t0" >> $tmpfile else cat /etc/fstab | grep -v $swapfile > $tmpfile fi -echo -e "# The lines below this one will be the new fstab file : \n\n $(cat $tmpfile)" | more +echo -e "# The lines below this one will be the new fstab file : \n\n$(cat $tmpfile)" | more while true ; do echo -e "$sep What to do with this fstab ?" - echo -e "[ \t\e[4ma\\e[0mccept changes | \e[4me\e[0mdit file | dis\e[4mc\e[0mard changes | show \e[4md\e[0miff ]" - read prompt + echo -e "[ \e[4ma\\e[0mccept changes | \e[4me\e[0mdit file | dis\e[4mc\e[0mard changes | show \e[4md\e[0miff ]" + read -p "[ a | e | c | d ]: " prompt case $prompt in a|accept)