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
RBASH(1)						      General Commands Manual							  RBASH(1)

NAME
rbash - restricted bash, see bash(1) RESTRICTED SHELL
If bash is started with the name rbash, or the -r option is supplied at invocation, the shell becomes restricted. A restricted shell is used to set up an environment more controlled than the standard shell. It behaves identically to bash with the exception that the follow- ing are disallowed or not performed: o changing directories with cd o setting or unsetting the values of SHELL, PATH, ENV, or BASH_ENV o specifying command names containing / o specifying a file name containing a / as an argument to the . builtin command o specifying a filename containing a slash as an argument to the -p option to the hash builtin command o importing function definitions from the shell environment at startup o parsing the value of SHELLOPTS from the shell environment at startup o redirecting output using the >, >|, <>, >&, &>, and >> redirection operators o using the exec builtin command to replace the shell with another command o adding or deleting builtin commands with the -f and -d options to the enable builtin command o using the enable builtin command to enable disabled shell builtins o specifying the -p option to the command builtin command o turning off restricted mode with set +r or set +o restricted. These restrictions are enforced after any startup files are read. When a command that is found to be a shell script is executed, rbash turns off any restrictions in the shell spawned to execute the script. SEE ALSO
bash(1) GNU Bash-4.0 2004 Apr 20 RBASH(1)
All times are GMT -4. The time now is 11:33 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy