1

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
6
  • Forgot to add, the aim of the script is to check the computers processor count and then change all references of the processor number in another script in accordance to that
    – Giles
    May 17, 2016 at 21:33
  • 1
    TBH, I do not understand why did you do this so complicated? Is not it easier just use $(nproc) inside test.sh where appropriate?
    – Serge
    May 17, 2016 at 21:42
  • It would, and i tried a few approaches with variables like this, but test.sh enters an environment that wont see the variables (but must remain in the environment for the script to work), the only way round it that i've found that works is to physically change the script using another script
    – Giles
    May 17, 2016 at 21:47
  • 3
    What error do you get? My guess is that your /bin/sh doesn't support if ((...)) May 17, 2016 at 22:36
  • 1
    Why are you doing echo | all over the place? In particular, why are you doing echo | ""? May 18, 2016 at 1:58

1 Answer 1

3

Try this script instead:

#!/bin/sh
sed -i -e "s/processors=[0-9]\+/processors=$(nproc)/g" ~/Scripts/test.sh

sed is a Stream EDitor. -i switch tells it to make all changes inplace, -e though optional, tells that the following argument is a sed script

5
  • I looked at sed and completely over looked the -i flag, and this single line is beautiful. Script is working great again, thank you. I've only been coding in the last couple of months with a spare hour or so in my evening, so my scripting is very clunky and poor at the moment, I'm trying to get more refined though.
    – Giles
    May 17, 2016 at 22:45
  • You probably don't need the \+ — it appears from the question that there is never more than one digit after processors= in ~/Scripts/test.sh. May 18, 2016 at 2:03
  • @G-Man tomorrow, the system could be updated to the hi-end server, what then?
    – Serge
    May 18, 2016 at 3:33
  • @Serge: OK, good point, but (1) you're answering a question different from the one that was asked, and (2) the script that the OP already has (to the extent that it works) assumes a single-digit number of processors. May 18, 2016 at 3:45
  • @G-Man OK, Ok, you are absolutely correct
    – Serge
    May 18, 2016 at 3:47

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .