LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
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 09-08-2008, 01:32 AM   #1
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Slackware 10.1/10.2/12, Ubuntu 12.04, Crunchbang Statler
Posts: 3,786

Rep: Reputation: 282Reputation: 282Reputation: 282
[bash]difference between script on command line and in file


Hi,

have to write a script; on the command line, it's (as one-liner):
Code:
time while read line; do echo -n "$line "; grep -P -m 1 "\t[A-Z]$line" logfile |wc -l; done < <(tail db.txt)
The time is only for debugging as is the tail and the script works

If I put it in a file
Code:
while read line;
do
    echo -n "$line ";
    grep -P -m 1 "\t[A-Z]$line" logfile |wc -l;
done < <(tail db.txt)
and execute with sh verify.sh, it complains about the last line
Code:
wim@btd-techweb01:~/log$ sh verify.sh
verify.sh: line 5: syntax error near unexpected token `<'
verify.sh: line 5: `done < <(tail db.txt)'
Can someone explain to me why ? I assume that the input redirection has to be outside the file, but it does not explain the why.

If I add '#!/bin/bash' at the beginning of verify.sh and make it executable, it works (again) as expected.

Note:
I will probably post some more questions as it needs some serious optimisation and a slight modification.
 
Old 09-08-2008, 01:56 AM   #2
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,311

Rep: Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040
Quote:
If I add '#!/bin/bash' at the beginning of verify.sh and make it executable, it works (again) as expected.
Quote:
sh verify.sh
I suspect your 'sh' is the POSIX shell and is NOT symlinked (instead) to bash. You should never assume it is. If you want bash, specify it explicitly (as you later did).
 
Old 09-08-2008, 02:04 AM   #3
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Slackware 10.1/10.2/12, Ubuntu 12.04, Crunchbang Statler
Posts: 3,786

Original Poster
Rep: Reputation: 282Reputation: 282Reputation: 282
Hi Chris,

thanks for the reply and the warning never to assume.

Code:
wim@btd-techweb01:~$ which sh
/bin/sh
wim@btd-techweb01:~$ ls -l /bin/sh
lrwxrwxrwx  1 root root 4 2006-04-19 14:29 /bin/sh -> bash*
wim@btd-techweb01:~$ ls -l /bin/bash
-rwxr-xr-x  1 root bin 666452 2004-11-04 00:30 /bin/bash*
wim@btd-techweb01:~$
Definitely symlinked to bash; any other thoughts?

Last edited by Wim Sturkenboom; 09-08-2008 at 02:06 AM.
 
Old 09-08-2008, 02:56 AM   #4
jschiwal
Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 655Reputation: 655Reputation: 655Reputation: 655Reputation: 655Reputation: 655
If bash is started as sh, it will enable posix behavior.

Try /bin/bash verify.sh instead. The error will go away.

Last edited by jschiwal; 09-08-2008 at 02:57 AM.
 
Old 09-08-2008, 03:20 AM   #5
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Slackware 10.1/10.2/12, Ubuntu 12.04, Crunchbang Statler
Posts: 3,786

Original Poster
Rep: Reputation: 282Reputation: 282Reputation: 282
Thank you, that indeed worked as expected.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
bash shell script read file line by line. Darren[UoW] Programming 51 07-04-2013 09:16 PM
bash script 'for each command line argument' true_atlantis Linux - Newbie 3 01-28-2009 02:51 PM
bash script to find the difference between the 4th element of each line in 2 files dontbugmeplz Programming 6 07-21-2008 09:05 AM
bash command for make difference between two txt file kkpal Linux - Newbie 7 05-12-2008 08:07 AM
Bash Script, no new line for echo command jorisb Linux - General 5 11-05-2005 01:08 AM


All times are GMT -5. The time now is 05:11 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration