LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-03-2009, 02:13 PM   #1
gagou7
LQ Newbie
 
Registered: Sep 2009
Distribution: Ubuntu 9.04 / Debian 5.0 / Backtrack 3
Posts: 27

Rep: Reputation: 0
[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}'
For example, after "grep ESSID", I get "ESSID="Wireless"" and after awk command I get just "Wireless".

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
Scan=`sudo iwlist wlan0 scan | grep ESSID`
# Save awk's command in AwkCommand
AwkCommand="awk 'BEGIN {FS=\"\\\"\";RS=\" \"}{print $"$i"}'"
# Send ESSID to awk's command
$Scan | $AwkCommand
But I get this error:

Code:
awk: 1: unexpected character '''
I read that some people have the same problem. Maybe awk don't accept the apostrophe in a script? Or have I to escape the apostrophe like \' (it doesn't work)?

Thank's a lot for your help !

Last edited by gagou7; 11-05-2009 at 04:32 PM.
 
Old 11-03-2009, 02:20 PM   #2
Disillusionist
Senior Member
 
Registered: Aug 2004
Location: England
Distribution: Ubuntu
Posts: 1,039

Rep: Reputation: 97
What happens if you try:
Code:
echo $Scan | $AwkCommand
[EDIT]
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}'

Last edited by Disillusionist; 11-03-2009 at 02:32 PM.
 
Old 11-03-2009, 02:37 PM   #3
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389
Hi,

Did you try:

"$Scan" | "$AwkCommand" ?
 
Old 11-03-2009, 02:45 PM   #4
Disillusionist
Senior Member
 
Registered: Aug 2004
Location: England
Distribution: Ubuntu
Posts: 1,039

Rep: Reputation: 97
You can echo your awk statement into a temporary file:
Code:
echo "awk 'BEGIN {FS=\"\\\"\";RS=\" \"}{print \$$i}'" > /tmp/$$
Then you can:
Code:
echo $Scan | /tmp/$$
So long as these two statements are part of the same shell (otherwise $$ will be a different process id)

Last edited by Disillusionist; 11-03-2009 at 02:46 PM.
 
Old 11-04-2009, 09:31 AM   #5
gagou7
LQ Newbie
 
Registered: Sep 2009
Distribution: Ubuntu 9.04 / Debian 5.0 / Backtrack 3
Posts: 27

Original Poster
Rep: Reputation: 0
Thank all for your help, but nothing works...

The base command:
Code:
sudo iwlist wlan0 scan | grep ESSID | awk 'BEGIN {FS="\"";RS=" "}{print $2}'
The base script (doesn't work):

Code:
i=2

Scan=`sudo iwlist wlan0 scan | grep ESSID`

AwkCommand="awk 'BEGIN {FS=\"\\\"\";RS=\" \"}{print $"$i"}'"

$Scan | $AwkCommand
The error:

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"

ESSID="linksys"
ESSID="netgear"




ESSID="2345-1145"
2. With awk's command, I would like to keep only the ESSID's name ("Wireless", "linksys",...). I just did not understand the workings of awk. I thought it worked like that:

Code:
echo $Scan| awk 'BEGIN {FS="\"";RS=" "}{print $2}'
It gives me "Wireless"

Code:
echo $Scan| awk 'BEGIN {FS="\"";RS=" "}{print $4}'
It gives me "linksys"

Code:
echo $Scan| awk 'BEGIN {FS="\"";RS=" "}{print $6}'
It gives me "netgear"...

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`

echo $Essid | awk 'BEGIN {FS="\"";RS=" "}{print $2}'
Note: The essid's name are one below the other, I don't know why. But if you do this below, all essid's name are on a line separated by spaces:

Code:
$AllEssid=`echo $Essid | awk 'BEGIN {FS="\"";RS=" "}{print $2}'`
echo $AllEssid
But if you want to change the $2 to anything else, this one work great:

Code:
Essid=`sudo iwlist wlan0 scan | grep ESSID`

i="\$2"

echo $Essid | awk 'BEGIN {FS="\"";RS=" "}{print '$i'}'
I searched for anything...

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

Last edited by gagou7; 11-04-2009 at 09:49 AM.
 
Old 11-04-2009, 11:55 AM   #6
Disillusionist
Senior Member
 
Registered: Aug 2004
Location: England
Distribution: Ubuntu
Posts: 1,039

Rep: Reputation: 97
I think we are over-engineering a solution.

How about:
Code:
sudo iwlist wlan0 scan | grep ESSID |while read l_line
do
   echo $l_line|cut -d'"' -f2
done
I know that awk runs a lot faster than cut, but we are not talking hundreds of entries here.

The following works (or seems to):
Code:
sudo iwlist wlan0 scan | grep ESSID |awk 'BEGIN{FS="\""} {print $2}'

Last edited by Disillusionist; 11-04-2009 at 02:20 PM.
 
Old 11-05-2009, 12:27 PM   #7
gagou7
LQ Newbie
 
Registered: Sep 2009
Distribution: Ubuntu 9.04 / Debian 5.0 / Backtrack 3
Posts: 27

Original Poster
Rep: Reputation: 0
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
#
# Configuration automatique du système
#
########################################

echo "Lecture en cours des points d'accès Wifi..."

EssidAll=`awk 'BEGIN {FS="=";RS=""}{print $3}' /home/<my_account>/.hearc/credential.pwd | sudo -S iwlist wlan0 scan | grep ESSID | awk 'BEGIN {FS="\"";ORS=" "}{print $2}'`
Essid="NotNull"

i=0

while [ "$Essid" != "" ]
do
	i=$(($i+1))
	pos="\$"$i
	Essid=`echo $EssidAll | awk 'BEGIN {FS=" "}{print '$pos'}'`
	
	# Si je suis à l'école...
	
	if [ "$Essid" = "HE-ARC" ]
	then
		echo "Configuration en cours...[HE-ARC]"

		gconftool -s /system/http_proxy/use_http_proxy -t bool true
		gconftool -s /system/proxy/mode -t string "manual"

		if [ -e /etc/apt/apt.conf.school ]
		then
			awk 'BEGIN {FS="=";RS=""}{print $3}' /home/<my_account>/.hearc/credential.pwd | sudo -S mv /etc/apt/apt.conf.school /etc/apt/apt.conf
		fi
		if [ -e /etc/hosts.school ]
		then
			awk 'BEGIN {FS="=";RS=""}{print $3}' /home/<my_account>/.hearc/credential.pwd | sudo -S mv /etc/hosts /etc/hosts.athome
			awk 'BEGIN {FS="=";RS=""}{print $3}' /home/<my_account>/.hearc/credential.pwd | sudo -S mv /etc/hosts.school /etc/hosts
		fi
		sleep 3
		break
	fi

	# Si je suis à la maison...

	if [ "$Essid" = "Wireless" ]
	then
		echo "Configuration en cours...[HOME]"

		gconftool -s /system/http_proxy/use_http_proxy -t bool false
		gconftool -s /system/proxy/mode -t string "none"

		if [ -e /etc/apt/apt.conf ]
		then
			awk 'BEGIN {FS="=";RS=""}{print $3}' /home/<my_account>/.hearc/credential.pwd | sudo -S mv /etc/apt/apt.conf /etc/apt/apt.conf.school
		fi
		if [ -e /etc/hosts.athome ]
		then
			awk 'BEGIN {FS="=";RS=""}{print $3}' /home/<my_account>/.hearc/credential.pwd | sudo -S mv /etc/hosts /etc/hosts.school
			awk 'BEGIN {FS="=";RS=""}{print $3}' /home/<my_account>/.hearc/credential.pwd | sudo -S mv /etc/hosts.athome /etc/hosts
		fi
		sleep 3
		break
	fi
done

# Si je suis ailleurs qu'à l'école ou à la maison...

if [ "$Essid" != "Wireless" -a "$Essid" != "HE-ARC" ]
then
	echo "Configuration en cours...[DEFAULT-UNKNOWN]"

	gconftool -s /system/http_proxy/use_http_proxy -t bool false
	gconftool -s /system/proxy/mode -t string "none"

	if [ -e /etc/apt/apt.conf ]
	then
		awk 'BEGIN {FS="=";RS=""}{print $3}' /home/<my_account>/.hearc/credential.pwd | sudo -S mv /etc/apt/apt.conf /etc/apt/apt.conf.school
	fi
	if [ -e /etc/hosts.school ]
	then
		awk 'BEGIN {FS="=";RS=""}{print $3}' /home/<my_account>/.hearc/credential.pwd | sudo -S mv /etc/hosts /etc/hosts.athome
		awk 'BEGIN {FS="=";RS=""}{print $3}' /home/<my_account>/.hearc/credential.pwd | sudo -S mv /etc/hosts.school /etc/hosts
	fi
fi
sleep 3
Note 1: I am Swiss and I speak French, so some things are in French.

Note 2: The file "credential.pwd" contains two lines. "username=<my_username>" and "password=<my_password>". It is used for mounting DFS directory too.

Last edited by gagou7; 11-05-2009 at 04:34 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
awk shell script error ApacheRoseXbones Linux - Newbie 5 07-18-2008 12:06 PM
shell command using awk fields inside awk one71 Programming 6 06-26-2008 04:11 PM
Shell Script / Awk help for a challenge cmontr Programming 30 06-03-2008 06:11 PM
sed and awk in shell script bondoq Linux - Newbie 14 07-27-2007 01:52 AM
Passing variables from AWK script to my shell script BigLarry Programming 1 06-12-2004 04:32 AM


All times are GMT -5. The time now is 09:08 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration