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

About writing shell scripts and making the most of your shell
Forum rules
Topics in this forum are automatically closed 6 months after creation.
Locked
bull.backup

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

Post by bull.backup »

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
Last edited by LockBot on Wed Dec 28, 2022 7:16 am, edited 1 time in total.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
User avatar
xenopeek
Level 25
Level 25
Posts: 29587
Joined: Wed Jul 06, 2011 3:58 am

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

Post by xenopeek »

Moved here as this is where the other users knowing bash visit more often.
Image
semicolon

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

Post by semicolon »

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.
User avatar
xenopeek
Level 25
Level 25
Posts: 29587
Joined: Wed Jul 06, 2011 3:58 am

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

Post by xenopeek »

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:
Image
semicolon

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

Post by semicolon »

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 :)
cfaj

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

Post by cfaj »

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.
Locked

Return to “Scripts & Bash”