简体   繁体   中英

BASH script to switch between PI modes

I'm trying to create a BASH script for my Pi3 along with a desktop icon that will change the amount of gpu_mem allocated and rename the icon to the mode that is not in use.

#!/bin/bash

s1=$(grep gpu_mem /boot/config.txt)
s2="gpu_mem=156"

if [ "$s1" == "$s2" ]
           then
                sudo sed -i 's,^\(gpu_mem=\).*,\1'512',' /boot/config.txt
                sed -i 's,^\(Name[en_GB.UTF-8]=\).*,\1'Desktop Mode',' \
                /home/pi/Desktop/GPUMode.desktop

                else
                sudo sed -i 's,^\(gpu_mem=\).*,\1'156',' /boot/config.txt
                sed -i 's,^\(Name[en_GB.UTF-8]=\).*,\1'Game Mode',' \
                /home/pi/Desktop/GPUMode.desktop
            fi

Now the memory allocation part of the script works fine, but when I execute the line of code below for testing purposes:

sed -i 's,^\(Name[en_GB.UTF-8]=\).*,\1'Desktop Mode',' \
/home/pi/Desktop/GPUMode.desktop

I get the error:

 sed: -e expression #1, char 34: unterminated `s' command 

Any Ideas?

Try this instead using GNU awk instead of GNU sed for inplace editing:

sudo awk -i inplace '
BEGIN { FS=OFS="="; game_mem=156; desk_mem=512 }
(ARGIND == 1) && ($1 == "gpu_mem") {
    if ( $2 == game_mem ) {
        $2 = desk_mem
        mode = "Desktop"
    }
    else {
        $2 = game_mem
        mode = "Game"
    }
}
(ARGIND == 2) && ($1 == "Name[en_GB.UTF-8]") {
    $2 = mode " Mode"
}
{ print }
' /boot/config.txt /home/pi/Desktop/GPUMode.desktop

Copy your files before trying it of course...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM