Sponsored Content
Top Forums Shell Programming and Scripting awk command in script gives error while same awk command at prompt runs fine: Why? Post 302455063 by catalys on Monday 20th of September 2010 05:08:18 PM
Old 09-20-2010
awk command in script gives error while same awk command at prompt runs fine: Why?

Hello all,

Here is what my bash script does: sums number columns, saves the tot in new column, outputs if tot >= threshold val:

Code:
> cat getnon0file.sh
#!/bin/bash
this="getnon0file.sh"
USAGE=$this" [filename threshold_value]
InFile="xyz.38"
Min="0.05"
#[ I removed the input check to shorten the code]

awk '{sum=0; for(n=2; n<=NF; n++){sum+=$n}; tot=(NF+1);$tot=sum; if(sum>=0.05); {print}}' $InFile > $InFile.non0

echo ----  $this over ---  Result saved into $InFile.non0

When I call the script at the prompt, I get an error:
Code:
> getnon0file.sh xyz.38 0.05
/path/getnon0file.sh: line 36: syntax error near unexpected token `('
/path/getnon0file.sh: line 36: `awk '{sum=0; for(n=2; n<=NF; n++){sum+=$n}; tot=(NF+1);$tot=sum; if(sum>=0.05); {print}}' $InFile > 
$InFile.non0'

When I run the awk command alone at the prompt, I obtain the results I want
Code:
> awk '{sum=0; for(n=2; n<=NF; n++){sum+=$n}; tot=(NF+1);$tot=sum; if(sum>=0.05); {print}}' xyz.38 > xyz.38.non0

Is there any way around what seems to me an attractable error?

Thanks.

Just in case you need it, the datafile (xyz.38) look like this:

Code:
code1_001 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.008 0.028 0.198 0.502 0.625
code1_002 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.009 0.066 0.158 0.190
code1_003 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.009 0.060 0.148 0.185
code1_004 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 0.992 0.954 0.676 0.192 0.000
code2_001 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.008 0.055 0.229 0.390
code2_002 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.004 0.024 0.108 0.193
code2_003 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.029 0.118 0.209


Last edited by radoulov; 09-20-2010 at 06:14 PM.. Reason: Code tags, please!
 

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script runs fine, but not in a cron

Okay, I have the following script that runs fine from a command line as well as an executable .sh file. It just moves any file/folder with movie* in the name to a folder called _Movies. The issue I'm running into is when it's call from a cron. find /mnt/HD_a2/BT/complete -iname "movie.*" -exec... (4 Replies)
Discussion started by: sammyk
4 Replies

2. Shell Programming and Scripting

Script Runs fine but not giving any output

Hi, My script is running with no erros but not giving any output can anyonehelp. #!/bin/ksh . /home/application/bin/application.env OUTFILE=Result.txt PROD_PASSWORD=`${GET_PWD} -f ${PWD_FILE_PATH} -s ${PROD_SERVER} -u ${PROD_USER}` echo "1)To get the book last loaded details " read... (7 Replies)
Discussion started by: jagadish_gaddam
7 Replies

3. Programming

getting Segmentation Fault (core dumped) error but Program runs fine.

i am executing following program int main() { char str; FILE * fp; int i=0; ... (4 Replies)
Discussion started by: bhavesh.sapra
4 Replies

4. Shell Programming and Scripting

Shell script runs fine in Solaris, in Linux hangs at wait command

HI, I have a strange problem. A shell script that runs fine on solaris. when i ported to linux, it started hanging. here is the core of the script CFG_FILE=tab25.cfg sort -t "!" -k 2 ${CFG_FILE} | egrep -v "^#|^$" | while IFS="!" read a b c do #echo "jobs output" #jobs #echo "jobs... (13 Replies)
Discussion started by: aksaravanan
13 Replies

5. Shell Programming and Scripting

Script runs fine manually but not in crontab

Hello Guys, I have scratched my head alot on this but couldn't find clue what's wrong. Can you please help me with this? My problem is as following. 1) When I manually execute following script it runs successfully with below output. bash-3.00# more smssend #!/bin/bash echo -e "<Request... (16 Replies)
Discussion started by: umarsatti
16 Replies

6. Shell Programming and Scripting

Part of the Shell script is not running via crontab, runs fine manually

Hello Team, As a part of my job we have made a script to automate a service to restart frequently. Script having two functions when executing it's should find the existing service and kill it, then start the same service . Verified the script it's working fine when executing... (18 Replies)
Discussion started by: gowthamakanthan
18 Replies

7. Shell Programming and Scripting

Getting Command not found error Even though Script is working fine

Hi friends, I am using below script to do some work. But even though script is working fine but while executing it i am getting command not found error. :( Here is the script :- #!/bin/sh Names="name.txt" ###main##### for LINE in `cat ${Names}` do ... (3 Replies)
Discussion started by: harpal singh
3 Replies

8. Shell Programming and Scripting

Script runs in command-line fine but times out in CRON?

Hi, I have a script that seems to run to completion when in the command-line, but when it is run using the cron, it seems to time out. They both start and run fine, but on the CRON it stops prematurely. The script hits an API every few seconds and grabs data. Does anyone have any idea on... (4 Replies)
Discussion started by: phpchick
4 Replies

9. Shell Programming and Scripting

Bash script with awk command ERROR

Hello im new here... Im trying to read file and create folders from words in it but i get this for loop error awk : line 3 : syntax error at or near for my code is.. #!/bin/bash begin for (( i=1;i<=5;i++)); do awk -v i=$i $0 { print $i } mkdir $i done {print $i} end {} i have... (7 Replies)
Discussion started by: boxstep
7 Replies
USLEEP(3)						   BSD Library Functions Manual 						 USLEEP(3)

NAME
usleep -- suspend execution for interval of microseconds LIBRARY
Standard C Library (libc, -lc) SYNOPSIS
#include <unistd.h> int usleep(useconds_t microseconds); DESCRIPTION
The usleep() function suspends execution of the calling process until either the number of microseconds specified by microseconds have elapsed or a signal is delivered to the calling process and its action is to invoke a signal catching function or to terminate the process. The suspension time may be longer than requested due to the scheduling of other activity by the system. The microseconds argument must be less than 1,000,000. If the value of microseconds is 0, then the call has no effect. RETURN VALUES
On successful completion, usleep() returns 0. Otherwise, it returns -1 and sets errno to indicate the error. ERRORS
The usleep() function may fail if: [EINVAL] The microseconds interval specified 1,000,000 or more microseconds. SEE ALSO
nanosleep(2), sleep(3) STANDARDS
The usleep() function conforms to X/Open Portability Guide Issue 4, Version 2 (``XPG4.2''). It later appeared in the POSIX standard, but in IEEE Std 1003.1-2004 (``POSIX.1'') it was marked as legacy and the use of nanosleep(2) was recommended instead. The IEEE Std 1003.1-2008 (``POSIX.1'') revision removed usleep() from the specification. HISTORY
The usleep() function appeared in 4.3BSD. BSD
April 29, 2010 BSD
All times are GMT -4. The time now is 05:10 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy