LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-14-2018, 05:54 PM   #1
äxl
Member
 
Registered: Feb 2013
Location: Germany, EU
Distribution: Debian (stable release)
Posts: 67

Rep: Reputation: 10
[solved] bash script - exit status of grep not correct?


hi all, i got this bash script but i can't get it to work:
Code:
#!/bin/bash
expressvpn status > /home/user/transmissionvpn.log
cat /home/user/transmissionvpn.log
grep connected /home/user/transmissionvpn.log >/dev/null
RESULT=$?
if [ $RESULT -eq 0 ]
then echo nice -n 10 transmission-gtk -m
else zenity --info --text "Get your VPN running!"
fi
here's the output if i run in it terminal:
Code:
~$ bash -x ~/transmissionvpn.sh 
+ expressvpn status
+ cat /home/user/transmissionvpn.log
Connected to xxx 

+ grep connected /home/user/transmissionvpn.log
+ RESULT=1
+ '[' 1 -eq 0 ']'
+ zenity --info --text 'Get your VPN running!'
the log file contains "connected" so what am i doing wrong?

my bash version:
GNU bash, version 4.4.12(1)-release (x86_64-pc-linux-gnu)

tia

Last edited by äxl; 05-15-2018 at 01:11 AM.
 
Old 05-14-2018, 06:59 PM   #2
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,148

Rep: Reputation: 4124Reputation: 4124Reputation: 4124Reputation: 4124Reputation: 4124Reputation: 4124Reputation: 4124Reputation: 4124Reputation: 4124Reputation: 4124Reputation: 4124
Quote:
the log file contains "connected"
No it doesn't.
 
1 members found this post helpful.
Old 05-14-2018, 07:01 PM   #3
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
Connected != connected
 
1 members found this post helpful.
Old 05-15-2018, 12:57 AM   #4
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 19,872
Blog Entries: 12

Rep: Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053
a few things:
  • yes, linux is case sensitive
  • the word "disconnected" also contains the word connected => maybe use 'grep -w'. anyhow, read 'man grep'
  • a return status of 1 actually means FALSE

what id'd do:
Code:
if grep -qw connected /home/user/transmissionvpn.log; then
    echo nice -n 10 transmission-gtk -m
else
    zenity --info --text "Get your VPN running!"
fi
 
1 members found this post helpful.
Old 05-15-2018, 01:10 AM   #5
äxl
Member
 
Registered: Feb 2013
Location: Germany, EU
Distribution: Debian (stable release)
Posts: 67

Original Poster
Rep: Reputation: 10
oh, sorry for the noise.
in my .bashrc i have aliased grep -Ei to grep and i forgot so i didn't find the culprit.
thanks all!
 
Old 05-15-2018, 01:53 AM   #6
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,821

Rep: Reputation: 1212Reputation: 1212Reputation: 1212Reputation: 1212Reputation: 1212Reputation: 1212Reputation: 1212Reputation: 1212Reputation: 1212
.bashrc is run in interactive bash, not in bash scripts.
 
Old 05-15-2018, 01:58 AM   #7
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 22,015

Rep: Reputation: 7340Reputation: 7340Reputation: 7340Reputation: 7340Reputation: 7340Reputation: 7340Reputation: 7340Reputation: 7340Reputation: 7340Reputation: 7340Reputation: 7340
.bashrc is evaluated only for interactive shells - if I remember well.
Also aliases are not expanded when the shell is not interactive (unless the expand_aliases shell option is set using shopt).

So your explanation is not clear for me. But never mind, if it solves your problem it is completely ok.

If you want to avoid - in general - such surprises use always the full path:
Code:
/bin/grep ....
# will not be evaluated as function, alias or any other hidden tricky thing
 
  


Reply

Tags
bash, grep, script, scripting



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] bash exit status irajjs Programming 13 11-16-2014 03:51 PM
LXer: Bash If statements, Exit Status and Comparison Operators, A beginners guide to bash scripting LXer Syndicated Linux News 0 06-29-2014 07:35 PM
Bash, using exit status for decisions action_owl Programming 3 03-03-2010 05:20 AM
Bash Exit Status and Scripts hawk__0 Linux - Software 11 10-06-2009 07:41 PM
get exit status from a C-program in Bash Yoko Programming 1 07-12-2005 04:33 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:37 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