Changing contents of text file

My laptop supports battery conservation mode, so that it only charges up to 60%, which is very handy.

To enable this I need to change the contents of the file from “0” to “1”; which i am doing by “sudo nano” opening the file, changing the contents and then saving.

What I would like to do is create an alias that changes the content of the file. For example, open a terminal and type “halfbattery” and it will then change the contents of the file to “1”. Then when I need a full charge, opening a terminal and typing “fullbattery”, which will in turn change the contents of the file to “0” so that I get a 100% charge.

Thanks for any help!

You need to write 2 files, that will be magically transformed into scripts that do exactly what you want.
Save the following as halfbattery.sh in your home directory.

#! /bin/sh
#

echo "level=1 > /path/to/file"
exit 0

Then do the same with a file “fullbattery” with level=0. Mind that this overwrites all of the file contents, so if there’s more in that file, you need to add it to the script.

Next, make both scripts executable:

chmod +x ~/*battery.sh

If you’ve done this right, typing the name of either script will write the contents of the script (the part between “”) to the file. I do suggest you start by testing this to a made-up file that doesn’t actually do anything before attempting the real thing. You don’t want to mess up your config unless you know what you’re doing :stuck_out_tongue:

4 Likes

Your script has a bug. You will just output the string to the console when you run that. You probably meant to do something like (substitute the relevant line):

echo "level=1" > /path/to/file

Sorry, on my phone and it doesn’t like special characters. The double quotes needs to be around what you want to output (literally level=1 in this case). The > says to redirect that output to something, and the /path/to/file names that something as the file in that path.

How about two aliases?

echo ‘alias halfbattery=“echo 1 > /path/to/file”’ > ~/.bash_aliases

echo ‘alias fullbattery=“echo 0 > /path/to/file”’> ~/.bash_aliases

Something like that?
(Shorter aliases might be easier, like haba and fuba, which look mor pleasing in themselves…)
Then open new terminal/bash session?

Phone editing bad, ‘ and “ in bad places…

1 Like

Yup, missed that. But it was written quickly w/o referring to documentation. My bad :stuck_out_tongue:

Personally, I’d probably go with a single ~alias~ scriptfile that just swaps the value of that /path/to/file instead. Something like:

#!/bin/sh
FILE=/path/to/file
if [ ! -r ${FILE} ] ; then
  echo "Can't read file ${FILE}.  Exiting" >& 2
  exit 1
fi

if ! grep -qe 'level=[0|1]' ${FILE} ; then
  echo "No level defined in ${FILE}.  Terminating" >&2
  exit 1
fi

VALUE=$(grep -e 'level=[0|1]' ${FILE} | cut -d= -f2)

if [ ${VALUE} -eq 1 ] ; then
  echo "level was ${VALUE}.  Setting level=0 in ${FILE}."
  echo "level=0" > ${FILE}
else
  echo "level was ${VALUE}.  Setting level=1 in ${FILE}."
  echo "level=1" > ${FILE}
fi
1 Like

I guess my way, if no file, it will be created. If file contains anything other than a single digit, it would be overwritten with the single digit.

Would OP need to initially create a file with either a 1 or a 0 in it?

(Not sure my way would even have worked, and had no handle for any errors / unexpected results at all)

Note - handling errors is why it takes about 5x as long to write “simple scripts” than you’d expect :wink:

1 Like