user7401297
user7401297

Reputation: 1

Calling another shell script without passing any arguments

Is it possible to execute another shell script by dynamic arguments or without passing any arguments? Can someone please help me with the below Scenario?

Note: The script2 has two input arguments

Scenario:

  1. I need to call the shell script2 from script1 without passing any arguments, in this case the script2 should consider the default value declared in script2.

  2. If I pass the second argument, the second script should use the second argument from script1 and first arguments should be the default value declared in script2.

Script:1

#!/bin/bash
IN_A="Value1"
IN_B="Value2"

Script:2

#!/bin/sh
Val1=${1:-1st default}
Val2=${2:-2nd default}

echo "val1: $Val1"
echo "val2: $Val2"
exit 1

The below script2 will be called inside the Script1

 ./Script2.sh $IN_B

Expected:

val1: 1st default
val2: Value2

Actual:

val1: Value2
val2: 2nd default

Upvotes: 0

Views: 353

Answers (1)

William Pursell
William Pursell

Reputation: 212288

Your question does not make much sense to me. If you want script2 to use default values, then write it like:

#!/bin/sh

val1=${1:-default value}
val2=${2:-2nd default}

When you call it with no arguments, the variables val1 and val2 will get their default values. If you call it with one argument, val1 will be assigned that argument, and val2 will get the default. If you call it with two arguments, each variable will take the value from the positional parameters. Whether you call it from the command line or from a script is not particularly relevant.

Upvotes: 1

Related Questions