LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   [SOLVED] Shell script using awk command (https://www.linuxquestions.org/questions/linux-newbie-8/%5Bsolved%5D-shell-script-using-awk-command-766561/)

gagou7 11-03-2009 02:13 PM

[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 !

Disillusionist 11-03-2009 02:20 PM

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}'

druuna 11-03-2009 02:37 PM

Hi,

Did you try:

"$Scan" | "$AwkCommand" ?

Disillusionist 11-03-2009 02:45 PM

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)

gagou7 11-04-2009 09:31 AM

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

Disillusionist 11-04-2009 11:55 AM

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}'

gagou7 11-05-2009 12:27 PM

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.


All times are GMT -5. The time now is 04:10 AM.