简体   繁体   中英

how to call a function with 2 arguments which under the option of “getopts”

New in Linux bash script. Here I tried to create some files with getopts . For example I'd like to create 3 files called xyzfile, in command line ./createfiles -n xyzfile 3 should be given (2 arguments after the option -n ). The result should be 3 files with the names xyzfile_1, xyzfile_2 and xyzfile_3.

I tried to put my createfile() function outside the while-loop and as well as inside the while-loop. But the option -n doesn't work. I also tried to create another function called foo() with included the function createfile() , but still something wrong there. I have no idea anymore what I can do. Hope I can get some advices from you guys. Thank you very much!

#!/bin/bash

    while getopts :n:bc opt; do
        case $opt in
            n) echo test 3333333
                 createfile() {
                    echo "$OPTARG"
                    sum=$2

                 for((i=1;i<=sum;i++))
                    do
                    touch "$OPTARG_${i}"
                done
                 }
                 createfile $OPTARG ${2};;
            b) echo "test 1111111";;
            c) echo "test 2222222";;
            *) echo error!;;
        esac
    done

Parse the options first, then use the values you discover. An option can take only a single argument, so -n only gets the first one (I'll keep that as the file-name stem here). The count will be an ordinary positional argument found after parsing the options.

while getopts :n:bc opt; do
  case $opt in
    n) stem=$OPTARG; shift 2;;
    b) shift 1;;
    c) shift 1;;
    *) shift 1; echo error ;;
  esac
done

count=${1?No count given}

createfile () {
  for ((i=$1; i<=$2; i++)); do
      touch "${1}_${i}"
  done
}


createfile "$stem" "$count"

Use a separate option for the count, and create your files after the option processing.

Something like:

while getopts "n:c:" opt; do
    case $opt in
        n) name="$OPTARG";;
        c) count=$OPTARG;;
        # other options...
    esac
done

shift $((OPTIND -1))

while (( count > 0 )); do
    touch "${name}_$count"
    (( count-- ))
    # ...
done

getopts supports only options without, or with one argument. So you'll have to decide on which way you want your script to work. You have multiple options:

  • add a new option -m or similar to pass the maximum number of files you want to create: createfile -n xyzfile -m 3
  • you can also use the arguments that are not passed as an option, if you do your parsing well then createfile 3 -n xyzfile or createfile -n xyzfile 3 would mean the same. In my scripts I often use such positional argument if there is one option that the user always needs to pass.
  • You might even consider changing your way of calling the script to createfile xyzfile -n 3 or even createfile xyzfile where the name is a positional argument and the number of files optional (choose a logical default value, probably 1)...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM