0

I am working on a BASHScript to start wlan monitor mode without interefering with NetworkManager and I am having trouble with this function:

serial_setup(){

local file_name=$1
while IFS =, read PhysicalDevice ItfAlias ItfMode;
do
iw $PhysicalDevice interface add $ItfAlias type $ItfMode
ifconfig $ItfAlias up
done < file_name

}

The above function reads a textfile in format:

PhysicalDevice1,alias1,mode1
PhysicalDevice2,alias2,mode2
PhysicalDevice3,alias3,mode3

I need to find out the cause of error and optimum solution?

EDIT: Here is the full code: MM Script I have to use paste`bin due to trouble pasting and formatting the code here

Rui F Ribeiro
  • 56,709
  • 26
  • 150
  • 232
Xasel
  • 103
  • 1
    how do you call / launch the bash script? – lese Jan 31 '17 at 13:42
  • well: ./myscript.sh <command_to_activate _the_fuction> <file_name>.Note;script and file both are in same directory – Xasel Jan 31 '17 at 13:57
  • It's really not helpful to obfuscate your input like <text_file.txt> and <command_to_activate _the_fuction> IMHO: show us what you're actually typing and the actual error that occurs. FWIW it looks like you're trying to call a function from a script by passing its name as the first argument to the script - I'm not aware that that works (at least not in bash): either source your script so that the function becomes available in the current shell, or add a line to your script itself that calls the function with the script's first argument. – steeldriver Jan 31 '17 at 17:31
  • And please double-check that you are showing us an exact, accurate representation of your script. There are at least a couple of trivial typos in the script in your question.  I suspect that they are typos in the question only, and they're not really in the script, but it would be nice to be sure. – G-Man Says 'Reinstate Monica' Feb 03 '17 at 06:44

1 Answers1

0

It looks to me as if you have forgotten a $ on a variable. You have also forgotten to double-quote several other variables:

serial_setup () {
    local file_name="$1"

    while IFS=, read PhysicalDevice ItfAlias ItfMode; do
        iw "$PhysicalDevice" interface add "$ItfAlias" type "$ItfMode"
        ifconfig "$ItfAlias" up
    done <"$file_name"
}

See "Security implications of forgetting to quote a variable in bash/POSIX shells".

Kusalananda
  • 333,661
  • hmm..sir its still not working after performing eadits as per your suggestions – Xasel Feb 05 '17 at 05:56
  • @Xasel You also had a space between IFS and =, that I didn't spot at first. For the future, never say it doesn't work, describe what's happening and what you had expected would happen. It makes it easier to help. – Kusalananda Feb 05 '17 at 07:45
  • UH atlast,this was the root of all problem(no pun intneded),thank you sire for correcting me out.Most of the tutorials that I referred to doesn't enlist nuances as such.Therefore I am deciding to buy a dedicated book on BAsh Intrenals.Can you suggest some good ones as per your experience? – Xasel Feb 05 '17 at 08:02
  • @Xasel No, sorry I can't. I've never used a shell scripting book. I've only looked at working examples and at the manual (man bash). – Kusalananda Feb 05 '17 at 08:16