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

function arguments javascript
python function multiple arguments
python function arguments list
python pass function as argument with parameters
how to call a function in python
calling function multiple times with different arguments python
function arguments python
python function arguments type

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 3should 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

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

Multiple Function Arguments - Learn Python, Every function in Python receives a predefined number of arguments, if declared So calling foo(1,2,3,4,5) will print out: def bar(first, second, third, **options):. Using named arguments makes it easier to keep track of which arguments you passed and which you omitted. Optional arguments are preceded by the Optional keyword in the procedure definition. You can also specify a default value for the optional argument in the procedure definition.

A Tutorial on Using Functions in R! (and their scoping), You have seen that function arguments are specified within the () . Here, specifying just n ( 2 in the snippet) causes the function to The first call does what expected, the second does not and  The parameters, in a function call, are the function's arguments. JavaScript arguments are passed by value: The function only gets to know the values, not the argument's locations. If a function changes an argument's value, it does not change the parameter's original value. Changes to arguments are not visible (reflected) outside the function.

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"

Review: Functions (article) | Functions, In order for the program to execute the code that's inside the function, we our sayHello function definition so it knows that it will receive 2 arguments, and then  When you call a procedure that takes more than one argument, you must provide the values of the arguments in the order they are listed inside of the parentheses. Fortunately, you don't have to. If you know the names of the arguments, you can type them in any order and provide a value for each.

Chapter 15. Functions, You can call a function by mentioning its name, followed by arguments in parentheses: var add = function ( x , y ) { return x + y }; console . log ( add ( 2 , 3 )); // 5 Another possibility is to hand in optional parameters as named parameters, as  Here, the function greet () has two parameters. Since we have called this function with two arguments, it runs smoothly and we do not get any error. If we call it with a different number of arguments, the interpreter will show an error message. Below is a call to this function with one and no arguments along with their respective error messages.

Function call by value in C programming, Function call by value in C programming : Actual parameters are copied to the formal parameters, Formal parameters: The parameters that appear in function declarations. Example 2: Swapping numbers using Function Call by Value. A Tutorial on Using Functions in R! The tutorial highlights what R functions are, user defined functions in R, scoping in R, making your own functions in R, and much more. In a previous post , you covered part of the R language control flow, the cycles or loop structures.

Creating Functions – Programming with R, Functions can accept arguments explicitly assigned to a variable name in the function call functionName(variable = value) , as well as arguments by order:. Function Arguments. You can call a function by using the following types of formal arguments − Required arguments; Keyword arguments; Default arguments; Variable-length arguments; Required arguments. Required arguments are the arguments passed to a function in correct positional order.

Comments
  • getopts only supports flags and single-argument options. As a general rule, don't try to do anything while parsing arguments; just record the values you find, and use those values after parsing is complete.
  • @chepner so I have to put the createfile() function outside of the while-loop? so how can i realize the result with the given conditions?
  • it works partly, if i put ./createfile.sh -c 3in command line, it works with the 3 files with the name 1, 2, 3 as result. other options or processes didn't work
  • @cchi: the name variable was ignored because of the underline following it. I corrected it now to ${name}_... So ./createfile.sh -n xyzfile -c 3 should now do what you expect. If you add other options in the case statement, don't forget to also add them to the getopts line.
  • this modification works great as i expected. One thing i don't understand is, as i already wrote a function named createfile() before without getopts but with a for- loop. in the for-loop it worked exactlly as i want for((i=1;i<=count;i++)) do touch "$name_${i}" done Because i think the variable count is actually the one whiched would be changed by every run but not the name. That's why i also tried to modify your code like touch "$name_${count}" of course it didn't work. I'd like to know why name not count?
  • @cchi The variable $name_ is a different variable than $name (and the former is undefined). That is why the brackets are needed: to separate the variable name from the litteral _ you want to use. The brackets woud not be needed if you would use $name$count, $name-$count, $name.$count etc. But _ is a valid character in a variable name so you need ${name}_...
  • got it. Thank you so much!
  • u r right. I just achieved the right solution for this problem. ./createflie.sh -n xyzfile -m 3 maybe the most suitable way to solve the problem. Thank you very much for your ideas and tipps!
  • @cchi if any of these answers solves, your problem, consider accepting it (checkmark) to indicate the solution to your problem. If other answers were helpful, you can upvote them to indicate that they are helpful and to say "thank you" :)
  • just gave the checkmark and also voted your answer. Thank you for your explanation!
  • in the for-loop it shows as an error with the i=$1, so i remove the $ and wrote just i=1, the result files are like _1, _2 without file name. seems like the ${1}in linetouch doesn't work @chepner
  • without -n just ./createfile.sh filename 2 works with the above result, with -ndoesn't work @chepner
  • I misspelled stem in the case statement.
  • I've also switched to shift arguments as they are read, rather than all at once after they are parsed.
  • I just modified your code from i=$1 to i=$1+1, so that it would not create filename_0. your anwer is also quite helpful. Thank you !