[SOLVED] Shell script using awk command
Hi everybody,
I try to do a shell script that scan the wifi's network around my computer. For that, I use this command: Code:
sudo iwlist wlan0 scan | grep ESSID | awk 'BEGIN {FS="\"";RS=" "}{print $2}' It's working fine if I type it directly in the shell. I would like to save all ESSID in a variable and then do some awk command on the variable. I'm trying to insert it into a script shell. I do some modification and I think this is the most correct synthax: Code:
# Save all ESSID in Scan Code:
awk: 1: unexpected character ''' Thank's a lot for your help ! |
What happens if you try:
Code:
echo $Scan | $AwkCommand Don't worry, it doesn't work (just tried) the following works but not sure if this really helps with what you are trying to achieve: Code:
echo $Scan | awk 'BEGIN {FS="\"";RS=" "}{print $2}' |
Hi,
Did you try: "$Scan" | "$AwkCommand" ? |
You can echo your awk statement into a temporary file:
Code:
echo "awk 'BEGIN {FS=\"\\\"\";RS=\" \"}{print \$$i}'" > /tmp/$$ Code:
echo $Scan | /tmp/$$ |
Thank all for your help, but nothing works...
The base command: Code:
sudo iwlist wlan0 scan | grep ESSID | awk 'BEGIN {FS="\"";RS=" "}{print $2}' Code:
i=2 Code:
awk: 1: unexpected character ''' My objectives: 1. Save the list of all ESSID in $Scan ("Scan=`sudo iwlist wlan0 scan | grep ESSID`" works fine). "echo $Scan" print this: Code:
ESSID="Wireless" Code:
echo $Scan| awk 'BEGIN {FS="\"";RS=" "}{print $2}' Code:
echo $Scan| awk 'BEGIN {FS="\"";RS=" "}{print $4}' Code:
echo $Scan| awk 'BEGIN {FS="\"";RS=" "}{print $6}' But no, I don't need to increment the $2 to $4,$6,$8...IThe command already give me all ESSID without the "ESSID=". Thus, I don't need to save the awk's argument.:-) I have just to do that (maybe with the sed command I can separate ESSID): Code:
Essid=`sudo iwlist wlan0 scan | grep ESSID` Code:
$AllEssid=`echo $Essid | awk 'BEGIN {FS="\"";RS=" "}{print $2}'` Code:
Essid=`sudo iwlist wlan0 scan | grep ESSID` But I don't know yet how to put a complete awk's command into a variable and then use it like a command...Problem with apostrophe I think. I'm curious if someone have an idea |
I think we are over-engineering a solution.
How about: Code:
sudo iwlist wlan0 scan | grep ESSID |while read l_line The following works (or seems to): Code:
sudo iwlist wlan0 scan | grep ESSID |awk 'BEGIN{FS="\""} {print $2}' |
All is working know !!!
Just an explanation: With my notebook I change often place because of school. But I'm still the same three locations: - At school (I have a proxy) - At home (I must modify my hosts file because I have a server). - Eslewhere (No proxy, default hosts file). For this reason, I make a shell script that configure my notebook automatically. I use the wi-fi for that. At school, I'have an AP called "HE-ARC", at home I have an AP called "Wireless" and for elsewhere I can't see "HE-ARC" or "Wireless". Here's the script: Code:
#!/bin/sh Note 2: The file "credential.pwd" contains two lines. "username=<my_username>" and "password=<my_password>". It is used for mounting DFS directory too. |
All times are GMT -5. The time now is 04:10 AM. |