HiveBrain v1.2.0
Get Started
← Back to all entries
patternbashMinor

Editing a playlist configuration file

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
configurationfileplaylistediting

Problem

What I'm trying to do here is first set the first eight variables to the proper default values. That's the first area I'm thinking there might be a way to optimize. Is there anyway to set both variables at once for example I know in PHP I could do $title=$d_title="value"; Is there any way to consolidate that section? (The variables prefixed with d_ will not change, but the same variable without the d_ may be changed.)

Then the rest of the snippet should do the following:

-
If the second argument is NOT "config"

  • Look in the file to see if the a line with the format ${sfile}_variablename="*" exists.



  • If it does, use sed to replace the contents of the quotes with the new variable



  • If it does not, append the line in its entirety



  • If the second argument IS "config"



  • If a line exists that contains ${sfile}_variablename, read in what's between the quotes and assign it to the variablename



  • If not, set the variablename to the default value



Below is how I was achieving that. Is there any way to do this in a more simplified or compact manner?

In this case, sdir is a base path I define elsewhere, and sfile is another prefix. Those are hard-coded into the top of the script, so those will always be correct.

```
d_finddir=${2:-"${sdir}/music/trine_soundtrack/"}
finddir=${d_finddir}

d_title=${3:-"Random Tangent"}
title=${d_title}

d_desc=${4:-"Random Tangent Playlist"}
desc=${d_desc}

d_rand=${5:-"randomize"}
rand=${d_rand}

if [ ! "${finddir}" = "config" ]; then
if egrep -q "${sfile}_finddir" "${sdir}/config.cfg"; then
sed -r -i "s,(${sfile}_finddir=).*,\\1\"${finddir}\"," "${sdir}/config.cfg"
else
printf "%s_finddir=\"%s\"\n" "${sfile}" "${finddir}" >>"${sdir}/config.cfg"
fi

if egrep -q "${sfile}_title" "${sdir}/config.cfg"; then
sed -r -i "s,(${sfile}_title=).*,\\1\"${title}\"," "${sdir}/config.cfg"
else
printf "%s_title=\"%s\"\n" "${sfile}" "${title}" >>"${sdir}/config.cfg"
fi

Solution

-
Using sed to its fullest

sed is surprisingly powerful tool. For example:

finddir=$(sed -n -E /"${sfile}_finddir"'/s/[^"]*\"([^"]*).*/\1/p' "${sdir}/config.cfg")
[ -z ${finddir} ] && finddir=${d_finddir}


achieves the desired effect without any egrep.

  • Explanation: The last line ([ -z ]) tests whether finddir has zero length (meaning that sed didn't find the line in interest) and if so (&&) assigns it a default value.



-
Editing the config file

I am not sure I understand the decision to edit the config file via the script. It is much more natural (at least within the context provided) to edit it manually.

Code Snippets

finddir=$(sed -n -E /"${sfile}_finddir"'/s/[^"]*\"([^"]*).*/\1/p' "${sdir}/config.cfg")
[ -z ${finddir} ] && finddir=${d_finddir}

Context

StackExchange Code Review Q#115830, answer score: 2

Revisions (0)

No revisions yet.