0

I have a shell script 8185556677 (bash shell on Rasbpian) , in a file:

#!/bin/sh
#setup network interfaces
setup_interfaces()
{
    INTERFACE_0=$1
    SSID_0=$2
    PSK_0=$3
    WIRELESS_POWER_0=$4
    echo "interface:" $INTERFACE_0 "ssid:" $SSID_0 "psk:" $PSK_0 "wireless_power:" $WIRELESS_POWER_0
    sudo cp -r /etc/network/interfaces /etc/network/interfaces.BACKUP
    sudo cp /home/pi/Edge-CommHub/Edge-CommHub/Edge-CommHub/resources/RASPIAN/interfaces.TEMPLATE /etc/network/interfaces
    sudo sed -i 's/<interface_0>/$INTERFACE_0/g' /etc/network/interfaces
    sudo sed -i 's/<interface_0_ssid>/$SSID_0/g' /etc/network/interfaces
    sudo sed -i 's/<interface_0_psk>/$PSK_0/g' /etc/network/interfaces
    sudo sed -i 's/<wireless_power_on_off>/$WIRELESS_POWER_0/g' /etc/network/interfaces
    sudo cp -r /etc/wpa_supplicant/wpa_supplicant.conf /etc/wpa_supplicant/wpa_supplicant.confBACKUP
    sudo cp -r /home/pi/Edge-CommHub/Edge-CommHub/Edge-CommHub/resources/RASPIAN/wpa_supplicant.confTEMPLATE /etc/wpa_supplicant/wpa_supplicant.conf
    sudo sed -i 's/<interface_0_ssid>/$SSID_0/g' /etc/wpa_supplicant/wpa_supplicant.conf
    sudo sed -i 's/<interface_0_psk>/$PSK_0/g' /etc/wpa_supplicant/wpa_supplicant.conf
}

setup_interfaces

And Im calling the script with input parameters:

./setup.sh <param1> <param2> <param3> <param4>

For example ./setup.sh wlan0 Wifi_2.G 8185556677 off

However the values are NOT being passed to the shell script & the output is:

interface: ssid: psk: wireless_power:

Rather than:

interface: wlan0 ssid: Wifi_2.G psk: 8185556677 wireless_power: off

Why are my command line arguments not reaching the script?

1 Answer 1

2

They are being passed to the script, you could see that if you put echo "$1" immediately before the line:

setup_interfaces

Where they're not being passed is from that line to your function. Within the function, $1..n are the arguments to the function, not the program.

The first call is the snippet below is basically what you're doing, the second call forwards the script arguments on to the function:

#!/usr/bin/env bash

myfunc() {
    for arg in "$@" ; do
        echo "   ${arg}"
    done
}

echo "No args:"
myfunc
echo "With args:"
myfunc "$@"

The output is shown in the following transcript:

pax> ./prog.sh a b c "d e f"
No args:
With args:
   a
   b
   c
   d e f

The solution, in your case, is as simple as calling the function with something like:

setup_interfaces "$1" "$2" "$3" "$4"

You may also want to properly use the variables within your function as well:

INTERFACE_0="$1"
SSID_0="$2"
PSK_0="$3"
WIRELESS_POWER_0="$4"
echo "interface: $INTERFACE_0 ssid: $SSID_0 psk: $PSK_0 wireless_power: $WIRELESS_POWER_0"

and realise that, within ' single quotes, variable expansion is not done, so you probably want " double quotes on the sed commands, such as:

sudo sed -i "s/<interface_0>/$INTERFACE_0/g" /etc/network/interfaces
Sign up to request clarification or add additional context in comments.

2 Comments

Don't use $*, use "$@" instead (including the quotes!). $* will not treat arguments with spaces correctly: myfunc a b "c d"
@Leonardo, hence my comment "There are better ways to ...". There was little need to do it the proper way when the OP's code in the function already assumed no spaces. But I'll update the answer to take your comment into account.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.