I made a script to check the computer's processor count and then change all references of the processor number in another script in accordance with that. I tested it first by copying and pasting it into the terminal. I can copy the entire script into the command line and it works. But when I run it as a script, it fails half way through.
Here is the script (I know it's clunky and there is definitely a simpler way of running it, but this was easy to write off the top of my head, if you have a better version of this script and a fix, then that would be awesome!)
#!/bin/sh
# This part works both in the script and when pasting into the terminal.
echo | nproc > ~/tmp/ProcessorNumber.txt
echo | perl -pi -e 's/processors=1/processors=8/g' ~/Scripts/test.sh
echo | perl -pi -e 's/processors=2/processors=8/g' ~/Scripts/test.sh
echo | perl -pi -e 's/processors=3/processors=8/g' ~/Scripts/test.sh
echo | perl -pi -e 's/processors=4/processors=8/g' ~/Scripts/test.sh
echo | perl -pi -e 's/processors=5/processors=8/g' ~/Scripts/test.sh
echo | perl -pi -e 's/processors=6/processors=8/g' ~/Scripts/test.sh
echo | perl -pi -e 's/processors=7/processors=8/g' ~/Scripts/test.sh
# This part of the script isn't working when run as script
# but is working when pasted into the command line.
if (( $(head -n1 ~/tmp/ProcessorNumber.txt) == 1 ))
then
echo | perl -pi -e 's/processors=8/processors=1/g' ~/Scripts/test.sh
else
echo | ""
fi
if (( $(head -n1 ~/tmp/ProcessorNumber.txt) == 2 ))
then
echo | perl -pi -e 's/processors=8/processors=2/g' ~/Scripts/test.sh
else
echo | ""
fi
if (( $(head -n1 ~/tmp/ProcessorNumber.txt) == 3 ))
then
echo | perl -pi -e 's/processors=8/processors=3/g' ~/Scripts/test.sh
else
echo | ""
fi
if (( $(head -n1 ~/tmp/ProcessorNumber.txt) == 4 ))
then
echo | perl -pi -e 's/processors=8/processors=4/g' ~/Scripts/test.sh
else
echo | ""
fi
if (( $(head -n1 ~/tmp/ProcessorNumber.txt) == 5 ))
then
echo | perl -pi -e 's/processors=8/processors=5/g' ~/Scripts/test.sh
else
echo | ""
fi
if (( $(head -n1 ~/tmp/ProcessorNumber.txt) == 6 ))
then
echo | perl -pi -e 's/processors=8/processors=6/g' ~/Scripts/test.sh
else
echo | ""
fi
if (( $(head -n1 ~/tmp/ProcessorNumber.txt) == 7 ))
then
echo | perl -pi -e 's/processors=8/processors=7/g' ~/Scripts/test.sh
else
echo | ""
fi
$(nproc)inside test.sh where appropriate?/bin/shdoesn't supportif ((...))echo |all over the place? In particular, why are you doingecho | ""?