Need Help with a Simple sed Replacement

I fumbled my way to a working bash script for using game-based configs in PCSX2, but the sed replacement to define the memory card file doesn’t work, and I don’t really know what I’m doing.

#!/bin/bash
## This script makes it possible to user individual per game configurationfiles for PCSX2.

pcsx2_ini="$HOME/.config/PCSX2/PCSX2-reg.ini"
game_configpath="$HOME/.config/PCSX2/game_configs"
memcardpath="$HOME/.config/PCSX2/memcards"
pcsx2_config_default="$HOME/.config/PCSX2/inis"
fullfilename=$1
filename=$(basename "$fullfilename")
title_id="${filename%.*}"
memcard="$title_id.ps2"

## If no parameter was passed set default path and exit
if [[ ! $1 ]]
then
        ## Exchange the configpath
        sed -i s@"^SettingsFolder=.*"@"SettingsFolder=\"$pcsx2_config_default\""@g "$pcsx2_ini"
        exit
fi

## When the config folder does not exist then create it and copy over the default inis from the default ini folder
if [[ ! -e "$game_configpath/$title_id"  ]]
then
        ## Create folder based on the ps2 exe id
        mkdir -p "$game_configpath/$title_id/"
        ## Copy over the default inis from default directory for a start.
        cp -a "$pcsx2_config_default"/*.ini "$game_configpath/$title_id/"
fi

## If game-specific memory card does not exist, then create it using blank default.
if [[ ! -e "$memcardpath/$memcard"  ]]
then
        ## Copy default memcard
        cp "$memcardpath"/default.ps2 "$memcardpath/$memcard"
fi

# Load relevant memory card
sed -i s@"^Slot1_Filename=.*"@"Slot1_Filename=$memcard" "$game_configpath/$title_id/PCSX2_ui.ini"

/usr/games/PCSX2 --fullscreen --fullboot --cfgpath="$game_configpath/$title_id" "$1"

I usually put a -e before the sed instruction. I’m not sure if it is required but give it a try.

I also make a copy of my source file in /tmp and test my sed commands on it without the in-place replace option, until I get it right.

1 Like

This is an inelegant workaround, but if I ensure that the default config has Slot1_Filename=default.ps2, this works:

sed -i "s|default.ps2|$memcard|g" "$game_configpath/$title_id/"PCSX2_ui.ini

In the script in your first post, there’s a @ missing. I get

sed: -e expression #1, char 43: unterminated `s’ command

which indicates this.

Here’s my final version if it’s of use to anyone else. Feel free to improve upon it.

#!/bin/bash
## This script makes it possible to user individual per game configurationfiles for PCSX2.
## Make sure your default PCSX2_ui.ini has Slot1_Filename set to default.ps2.
## Works with PCSX2 1.5.0
## Credit to parasven for the RetroPie script that was the basis for this.

pcsx2_ini="$HOME/.config/PCSX2/PCSX2-reg.ini"
game_configpath="$HOME/.config/PCSX2/game_configs"
memcardpath="$HOME/.config/PCSX2/memcards"
pcsx2_config_default="$HOME/.config/PCSX2/inis"
fullfilename=$1
filename=$(basename "$fullfilename")
title_id="${filename%.*}"
memcard="$title_id.ps2"

## If no parameter was passed set default path and exit
if [[ ! $1 ]]
then
        ## Exchange the configpath
        sed -i s@"^SettingsFolder=.*"@"SettingsFolder=\"$pcsx2_config_default\""@g "$pcsx2_ini"
        exit
fi

## When the config folder does not exist then create it and copy over the default inis from the default ini folder
if [[ ! -e "$game_configpath/$title_id"  ]]
then
        ## Create folder based on the ps2 exe id
        mkdir -p "$game_configpath/$title_id/"
        ## Copy over the default inis from default directory for a start.
        cp -a "$pcsx2_config_default"/*.ini "$game_configpath/$title_id/"
fi

## If game-specific memory card does not exist, then create it using blank default.
if [[ ! -e "$memcardpath/$memcard"  ]]
then
        ## Copy default memcard
        cp "$memcardpath"/default.ps2 "$memcardpath/$memcard"
fi

echo "memcard = $memcard"

# Load relevant memory card
sed -i "s|default.ps2|$memcard|g" "$game_configpath/$title_id/"PCSX2_ui.ini

/usr/games/PCSX2 --fullscreen --fullboot --cfgpath="$game_configpath/$title_id" "$1"

Added an exit trap so Attract Mode’s ‘Exit Emulator’ hotkey works correctly. attract must be launched via terminal to work correctly with this script.

#!/bin/bash
## This script makes it possible to user individual per game configuration files for PCSX2.
## Make sure your default PCSX2_ui.ini has Slot1_Filename set to default.ps2.
## This script only seems to work correctly with Attract Mode when attract is launched via terminal.
## Works with PCSX2 1.5.0.
## Credit to parasven for the RetroPie script that was the basis for this.

pcsx2_ini="$HOME/.config/PCSX2/PCSX2-reg.ini"
game_configpath="$HOME/.config/PCSX2/game_configs"
memcardpath="$HOME/.config/PCSX2/memcards"
pcsx2_config_default="$HOME/.config/PCSX2/inis"
fullfilename=$1
filename=$(basename "$fullfilename")
title_id="${filename%.*}"
memcard="$title_id.ps2"

## If no parameter was passed, set default path and exit.
if [[ ! $1 ]]
then
        ## Exchange the configpath
        sed -i s@"^SettingsFolder=.*"@"SettingsFolder=\"$pcsx2_config_default\""@g "$pcsx2_ini"
        exit
fi

## When the config folder does not exist, create it and copy over the default inis from the default ini folder.
if [[ ! -e "$game_configpath/$title_id"  ]]
then
        ## Create folder based on the filename of the game.
        mkdir -p "$game_configpath/$title_id/"
        ## Copy over the default inis from default directory for a start.
        cp -a "$pcsx2_config_default"/*.ini "$game_configpath/$title_id/"
fi

## If game-specific memory card does not exist, then create it using blank default.
if [[ ! -e "$memcardpath/$memcard"  ]]
then
        ## Copy default memcard
        cp "$memcardpath"/default.ps2 "$memcardpath/$memcard"
fi

echo "memcard = $memcard"

# Load relevant memory card.
sed -i "s|default.ps2|$memcard|g" "$game_configpath/$title_id/"PCSX2_ui.ini

# Launch PCSX2 using the relevant config folder.
/usr/games/PCSX2 --fullscreen --fullboot --cfgpath="$game_configpath/$title_id" "$1" &

# Close PCSX2 when Attract Mode's 'Exit Emulator' hotkey is pressed.  
cleanup() {
        echo "Caugh Signal ... terminating PCSX2."
        pkill PCSX2
        exit
}
trap cleanup INT TERM
read var
cleanup