LinuxQuestions.org
Visit Jeremy's Blog.
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 03-08-2012, 11:17 AM   #1
JohnyDRipper
LQ Newbie
 
Registered: Nov 2004
Location: EU
Distribution: Gentoo - tarball 1 install :-)
Posts: 14

Rep: Reputation: 0
[bash][awk] command runs from shell, not from script


I'm writing a script in bash and basically I want to read certain lines from html files that start with a date. I want these lines in an array (X). I use this awk command, but for some reason it won't execute properly from within the script.
Code:
declare -a TITLES
ArrayCounter=0
LineCounter=1
while [ $LineCounter -le ${#CNNUMMERS} ]; do
#    commando="grep -o -E ^$Day.*$Year[.].* $FILE_TEMP | awk 'NR == $LineCounter {print}' -"
    commando="awk -F\<br\>\<br\> '/$Day.*$Year/ {print \$1}' $FILE_TEMP | awk 'NR == $LineCounter {print}'"
    echo Loop is running for the $LineCounter th time.
    echo The command is $commando
    TITLES[$ArrayCounter]=`$commando`
    echo We already added $LineCounter titles to the array: ${TITLES[$ArrayCounter]}
    ((ArrayCounter=$ArrayCounter+1))
    ((LineCounter=$LineCounter+1))
done
This is the output:
Code:
Loop is running for the 1 th time.
The command is awk -F\<br\>\<br\> '/30.*1994/ {print $1}' /tmp/tmpscript | awk 'NR == 1 {print}'
awk: syntax error at source line 1
 context is
    >>> ' <<<
awk: bailing out at source line 1
We already added 1 titles to the array:
Loop is running for the 2 th time.
The command is awk -F\<br\>\<br\> '/30.*1994/ {print $1}' /tmp/tmpscript | awk 'NR == 2 {print}'
awk: syntax error at source line 1
 context is
    >>> ' <<<
awk: bailing out at source line 1
We already added 2 titles to the array:
Loop is running for the 3 th time.
The command is awk -F\<br\>\<br\> '/30.*1994/ {print $1}' /tmp/tmpscript | awk 'NR == 3 {print}'
awk: syntax error at source line 1
 context is
    >>> ' <<<
awk: bailing out at source line 1
We already added 3 titles to the array:
If I copy paste the command echo'd in the output, It runs as it's supposed to run though :s

I'm running the script on an old Apple PowerBook, OS X Leopard, Darwin 9.8.0

tia for any help!
 
Old 03-08-2012, 11:41 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Try putting set -xv at the start of the script and see what answers you find.
 
Old 03-09-2012, 05:01 AM   #3
Reuti
Senior Member
 
Registered: Dec 2004
Location: Marburg, Germany
Distribution: openSUSE 15.2
Posts: 1,339

Rep: Reputation: 260Reputation: 260Reputation: 260
The quotation marks are usually interpreted by the shell, and only the stuff inside forwarded to awk. But here they are forwarded directly and awk doesn’t understand that. It can be parsed again by the eval shell builtin:
Code:
$ TITLES[$ArrayCounter]=`eval $commando`
 
  


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
help with a shell script that runs 'dos2unix' command on files in a directory, keerti Linux - Newbie 6 01-30-2012 10:06 AM
[SOLVED] AWK runs in command line, but not in script PaulVanLil Linux - Newbie 4 03-03-2011 07:07 AM
[SOLVED] Shell script using awk command gagou7 Linux - Newbie 6 11-05-2009 12:27 PM
bash shell script using pidof, ps -ef, awk Joe1989 Linux - General 2 04-22-2009 11:01 AM
what is the script which runs at the exit bash shell supersubu123 Linux - General 6 01-24-2007 12:25 PM

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

All times are GMT -5. The time now is 02:16 PM.

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