Note that there are some explanatory texts on larger screens.

plurals
  1. POthis BASH code works on a command line, but not when i move it into a script
    text
    copied!<p>I built-up a kludgey script on the CLI that I can't successfully move into a text file. <strong>I know it's poorly-written, but I'm interested in resolving the problem I'm having, not re-writing it.</strong> I want to understand why my poorly-written code behaves differently when executed on the command line compared to when it's saved as a file.</p> <p>I think it's related to something getting interpolated prematurely in the script, because if I substitute the one-liner version into the same file (without formatting) I get identical output. Note the debug line below and how it doesn't print.</p> <p><strong>CONTENTS OF SCRIPT</strong></p> <pre> #!/bin/bash totalNodes=0 #initialize sum echo -e "ID\t\t\tNODES"; #title line for n2 in $( #n2 will get populated with the number of nodes (to be added to totalNodes) for n in $( #n is an intermediate string that munges the Resource_list.nodes line (kludgy) for i in $( qstat|grep " ef "|cut -f 1 -d ' ') #i gets the ef queue lines do echo "debug $i" j=$(echo $i|sed 's/(\d+)\..+/\$1/'); #j is the actual ID qstat -f | grep -A43 $j; #extracts the full output from qstat for this job ID done|grep Resource_List.nodes) #closes definition of n over loop do echo ${n};done|grep ppn) #closes definition of n2 over loop do echo "$j ${n2:0:1}" #output line totalNodes=$(($totalNodes+${n2:0:1})) #counting nodes done echo "$totalNodes nodes of 16 running in EF queue" </pre> <p><strong>EXPECTED OUTPUT</strong> (what I get at the command line):</p> <pre> ID NODES 2378512.yaddayadday-adm 4 2378512.yaddayadday-adm 4 2378512.yaddayadday-adm 4 2378512.yaddayadday-adm 2 14 nodes of 16 running in EF queue </pre> <p><strong>CURRENT OUTPUT</strong> from the script</p> <pre> ID NODES 4 4 4 2 14 nodes of 16 running in EF queue </pre> <p>So I'm confused how I can get the right total (meaning $n2 is getting defined correctly) but I can't even print $i on the debug-with-print line (line 8.)</p> <p>For reference, here's the one-liner. Like I said, <strong>this gives the "EXPECTED OUTPUT" shown above, when executed on the command line, but the same output as the above codeblock "CONTENTS OF SCRIPT" when I save it as a file with no additional formatting.</strong></p> <pre> totalNodes=0;echo -e "ID\t\t\tNODES";for n2 in $(for n in $(for i in $(qstat|grep " ef "|cut -f 1 -d ' ');do j=$(echo $i|sed 's/(\d+)\..+/$1/');qstat -f | grep -A43 $j;done|grep Resource_List.nodes);do echo ${n};done|grep ppn);do echo "$j ${n2:0:1}";totalNodes=$(($totalNodes+${n2:0:1})); done;echo "$totalNodes nodes of 16 running in EF queue" </pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload