1

I am trying to run bash script which run 8 for loops simulatenously.

one of the my for loop likes below

for i in 00 01 02 03 04 ; do cat $INDIR/sys*$1.$i*csv | awk -F '\xac' 'BEGIN{OFS=";";} ($4 == 10)  { $1=$1; print }'> $OUTDIR/$1_$i.csv

So I have converted into following syntax to run with nohup

nohup bash -c 'for i in 00 01 02 03 04; do cat $INDIR/rti*$1.$i*csv|awk -F \'\\xac\' \'BEGIN{OFS=\";\";} ($4==10) { $1=$1; print }\'>$OUTDIR/$i.csv;done' &

But I am getting following error

test.sh: line 9: syntax error near unexpected token `('
test.sh: line 9: `nohup bash -c 'for i in 00 01 02 03 04; do cat $INDIR/rti*$1.$i*csv|awk -F \'\\xac\' \'BEGIN{OFS=\";\";} ($4==10) { $1=$1; print }\'>$OUTDIR/$i.csv;done' & '

I also tried with following code changes

nohup sh -c ''

nohup /bin/bash -c ''

\'\xac\'

but no progress,

BTW my for loop works without nohup

Thanks for your help

  • The root issue here is the single quotes, backslashes don't work inside them, so you have 'for ...|awk -F \' quoted, then the rest is unquoted since the following single-quotes are all escaped. – ilkkachu Dec 6 '17 at 20:22
2

It would be much easier (read: more read- and maintainable) if you divide your code into two scripts: One to execute the actual function(s), and one to start them. Sample code for the latter:

for i in 00 01 02 03 04; do
    nohup /path/to/workerScript.sh ${i}
done

If you want to keep the logic in a single script you can still use this approach:

if [ $# -eq 0 ]; then
    for i in 00 01 02 03 04; do
        nohup $0 ${i}
    done

    exit 0
fi

# Rest of the logic follows
  • Hello Murphy I already have this solution and it works very well. But I am looking one script way :) thanks for your reply – Murat Dec 8 '17 at 12:49
  • @Murat You can have that and still keep the script readable. I expanded my example. – Murphy Dec 8 '17 at 13:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.