LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
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 06-21-2012, 04:42 PM   #1
firebirdsql
LQ Newbie
 
Registered: Dec 2011
Posts: 9

Rep: Reputation: Disabled
batch script help


In my batch script, I want to check the output of a command and see if it contains a string and if so, run another command.

Basically, my wifi doesn't turn on sometimes during resume mode. I want to call the iwconfig command and check whether the output contains the string "Access Point" and if so run a command.

This is what I have so far:
Code:
#!/bin/bash
if [ `echo iwconfig || grep 'Access Point' `] then;
        ifdown wlan0;
        ifup wlan0;
fi;
 
Old 06-21-2012, 04:54 PM   #2
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
1 - You don't want to "echo iwconfig", because that will print the literal string "iwconfig" to the output, rather than actually running the program iwconfig
2 - You don't want double || between the iwconfig and the grep, double || is an "or" statement, if you want to grep the output of the iwconfig you would use a single |
3 - You can't run an if statement on this command anyway. If you run iwconfig | grep "Access Point", the result will either be "Access Point" or just a blank string, either way the if statement won't know what to do with it.
4 - You don't need a semicolon after each line

I would use the -q flag in grep, then check the exit status

Code:
#!/bin/bash
iwconfig | grep -q "Access Point"
if [[ $? -eq 0 ]]; then
   ifdown wlan0
   ifup wlan0
fi
 
2 members found this post helpful.
Old 06-21-2012, 05:10 PM   #3
firebirdsql
LQ Newbie
 
Registered: Dec 2011
Posts: 9

Original Poster
Rep: Reputation: Disabled
Nevermind, I was running the script by calling sh wifi instead of just wifi.

Thanks

Last edited by firebirdsql; 06-21-2012 at 05:13 PM.
 
Old 06-21-2012, 05:11 PM   #4
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
What distro is this?

You can try switching from double brackets to single:
Code:
#!/bin/bash
iwconfig | grep -q "Access Point"
if [ $? -eq 0 ]; then
   ifdown wlan0
   ifup wlan0
fi
 
1 members found this post helpful.
Old 06-22-2012, 02:03 PM   #5
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Actually, you don't need any test brackets at all, if statements operate on the exit code of whatever command (or command list) you supply after the initial keyword.

Code:
if iwconfig | grep -q "Access Point" ; then
	ifdown wlan0
	ifup wlan0
fi

More generally, however, when using bash or ksh, it's recommended to use [[..]] for string/file tests, and ((..)) for numerical tests. Avoid using the old [..] test unless you specifically need POSIX-style portability.

http://mywiki.wooledge.org/ArithmeticExpression
http://mywiki.wooledge.org/BashFAQ/031
http://wiki.bash-hackers.org/commands/classictest
http://wiki.bash-hackers.org/syntax/...nal_expression
 
  


Reply



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
[SOLVED] batch script Rameses Linux - Newbie 5 07-14-2011 07:36 PM
Variables in Batch script gbothma Programming 5 07-28-2009 07:23 AM
Change batch script to shell script alan.belizario Programming 5 03-31-2005 12:41 AM
Change batch script to shell script alan.belizario Linux - Software 1 03-30-2005 01:49 AM
Batch mv and tr script ... OrangeWarp Linux - General 1 12-11-2002 05:14 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 05:28 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
Open Source Consulting | Domain Registration