I built-up a kludgey script on the CLI that I can't successfully move into a text file. I know it's poorly-written, but I'm interested in resolving the problem I'm having, not re-writing it. 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.
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.
CONTENTS OF SCRIPT
#!/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" EXPECTED OUTPUT (what I get at the command line):
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
CURRENT OUTPUT from the script
ID NODES 4 4 4 2 14 nodes of 16 running in EF queue
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.)
For reference, here's the one-liner. Like I said, 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.
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"
for n{2} in $(...with a|while read LINE; do case "$LINE in"... you can do the same with the output of qstat and operate on streams instead of ballooning the memory with unneeded large variables that is nearly as painful to read as recursion but with no benefit ... the read command can replace cut as well by usingread dummy0 dummy1 needed_var dummy2;jis used iteratively internally before the outer loop parses n2. How would it fit to be placed inecho "$j ${n2:0:1}"?echo "debug $i" >> tmp.outor"your_oneliner" > tmp.out; cat tmp.outThis can also be used for intermediate results, to iterate over withread while.jis already set to2378512.yaddayadday-admin your shell before you execute the one-liner? Do anecho $jto find out. You could add some sample input to your question.