Expected loop does not happens in a bash script. :shock:

Forum rules
Before you post please read this

Expected loop does not happens in a bash script. :shock:

Postby bull.backup on Tue Mar 19, 2013 3:21 pm

Hello.
Probably this be a silly question, for this reason I'm posting it here, instead of the Bash forum.
This script instead of the loop, opens and closes the terminal screen so fast that I can not see what it did. :shock:
The script works fine if I remove the string `date "+%D %R"` from the echo command line.
Also, if I type manually the two lines of the loop in a terminal, I get the expected answer.

Any help will be fully apreciated.
Thanks in advance.
Juan

#!/bin/bash

for (( ; ; ))
do
mac=$(ifconfig wlan0 | grep HWaddr | sed 's/.*HWaddr //')
echo `date "+%D %R"`" MAC ADDRESS local es $mac"
sleep 5m
done
bull.backup
Level 1
Level 1
 
Posts: 3
Joined: Sat Mar 16, 2013 7:48 pm

Linux Mint is funded by ads and donations.
 

Re: Expected loop does not happens in a bash script. :shock:

Postby xenopeek on Tue Mar 19, 2013 3:44 pm

Moved here as this is where the other users knowing bash visit more often.
User avatar
xenopeek
Level 21
Level 21
 
Posts: 14565
Joined: Wed Jul 06, 2011 3:58 am
Location: The Netherlands

Re: Expected loop does not happens in a bash script. :shock:

Postby semicolon on Tue Mar 19, 2013 4:09 pm

I don't see any problems and it also works perfectly fine for me. What happens if you call it from a terminal?

Code: Select all
sh yourscript.sh


edit:
Should have tried that myself first :wink: It reports a syntax error as it doesn't like your for loop and I totally agree that it isn't nice.

Instead of your for loop, try something like:
Code: Select all
while true;
do
    <code>
done

Funny really as i ran your code in terminal using './yourscript.sh' and it ran perfectly fine, always believed both did the same thing but apparantly I was wrong.
semicolon
Level 2
Level 2
 
Posts: 58
Joined: Sat Mar 09, 2013 7:26 am
Location: Netherlands

Re: Expected loop does not happens in a bash script. :shock:

Postby xenopeek on Tue Mar 19, 2013 5:11 pm

semicolon wrote:I don't see any problems and it also works perfectly fine for me. What happens if you call it from a terminal?

Code: Select all
sh yourscript.sh

Perhaps try bash yourscript.sh instead; running it with sh while you are using bash syntax is not funny :wink:
User avatar
xenopeek
Level 21
Level 21
 
Posts: 14565
Joined: Wed Jul 06, 2011 3:58 am
Location: The Netherlands

Re: Expected loop does not happens in a bash script. :shock:

Postby semicolon on Tue Mar 19, 2013 5:50 pm

xenopeek wrote:Perhaps try bash yourscript.sh instead; running it with sh while you are using bash syntax is not funny :wink:

Good point, usually it is no problem at all though. What happens if invoked with sh, is that bash mimics the behaviour of older versions of sh. Generally not a good habit though.

I'm more into shell than bash anyways, and... my script works in both bash and sh :P

To summarize: your script should work fine, if it doesn't try run it in the terminal :)
semicolon
Level 2
Level 2
 
Posts: 58
Joined: Sat Mar 09, 2013 7:26 am
Location: Netherlands

Re: Expected loop does not happens in a bash script. :shock:

Postby cfaj on Sat May 11, 2013 8:11 am

xenopeek wrote:
semicolon wrote:I don't see any problems and it also works perfectly fine for me. What happens if you call it from a terminal?

Code: Select all
sh yourscript.sh

Perhaps try bash yourscript.sh instead; running it with sh while you are using bash syntax is not funny :wink:

The only difference when bash is run as sh is the scripts it reads on invocation; all language features are still enabled.

Note that /bin/sh on Debian-based distros (such as Mint) is not bash; it is dash.

Try this:

Code: Select all
sudo ln /bin/bash /usr/local/bin/sh
PATH=/usr/local/bin:$PATH
sh -c 'q=qwert; echo "${q/w/W}"'


You will see that a bash feature not present in dash, in this case search and replace parameter expansion, is still available.
cfaj
Level 1
Level 1
 
Posts: 3
Joined: Sun May 05, 2013 6:13 pm


Return to Scripts & Bash

Who is online

Users browsing this forum: No registered users and 1 guest