I am changing a bash script which has a structure as such:
#somewhere in the code
sim_counts=#... some value
function_name()
{
set $sim_counts
for hostname in $linux_hostnames; do
if [ $1 -eq 0 ]; then # if sim_counts equal 0
shift # jump forward in sim_counts
continue
fi
# ... more code
shift
done
}
Then it is called in the script:
function_name
I want to introduce a parameter to this function:
#somewhere in the code
sim_counts=#... some value
function_name()
{
ip=$1
set $sim_counts
for hostname in $linux_hostnames; do
if [ $1 -eq 0 ]; then # if sim_counts equal 0
shift # jump forward in sim_counts
continue
fi
# ... more code
shift
done
}
And call the function in following way:
function_name 10.255.192.123
What should I do to avoid $1 conflict of function parameter and the other value from set command ?
If I am correctly reading the set builtin page in the Bash Reference Manual, I believe the code as you have written it will just work. Quoting from that page:
In essence, any pre-existing values for the positional variables will be blown away. The first sentence on that manual page is also interesting:
In short, I think your code should simply work as expected. You’ve saved the initial value of
$1(from the function call) into a temporary variable; as long as you refer to$ipfor that specific value, you should be good. In my own test script, it seems that$1gets blown away as I expect it should.