Pregunta

Okay so I don't quite understand why the for loop wont seq until the number used for the chkNum variable. To run this I use firstScript.sh -n3 -t10 userName. So I'm trying to remove the -n from the -n3 and run the for loop until 3, or whatever number is used there. Hopefully that wasn't too confusing, thanks in advance.

#!/bin/sh

chkNum=$1 #$1 line parameter
user=$3 #$3 line paramater
time=$2 #$2 line parameter

echo $chkNum|cut -c3-

for i in $(seq 1 $chkNum|cut -c3-)
do
   echo iVal: $i
done
¿Fue útil?

Solución

I'd use getopts, then the -n and -t options can arrive in any order:

while getopts :n:t: opt; do
    case $opt in
        n) num=$OPTARG;;
        t) time=$OPTARG;;
        :) echo "option -$OPTARG requires an argument"; exit;;
        *) echo "invalid option: -$OPTARG"; exit;;
    esac
done
shift $((OPTIND - 1))

for ((i=1; i<=$num; i++)); do
    ...
done
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top