Skip to main content
added 66 characters in body; edited title
Source Link
200_success
  • 145.6k
  • 22
  • 191
  • 481

Is there any way this bash script segment can be simplified? Editing a playlist configuration file

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.

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";$title=$d_title="value"; Is there any way to consolidate that section? (The variables prefixed with 'd_'d_ will not change, but the same variable without the 'd_'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

  • 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.

Is there any way this bash script segment can be simplified?

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.

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?

Editing a playlist configuration file

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.

Source Link
Trel
  • 227
  • 1
  • 6

Is there any way this bash script segment can be simplified?

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.

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?

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

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

    if egrep -q "${sfile}_mode" "${sdir}/config.cfg"; then
        sed -r -i "s,(${sfile}_mode=).*,\\1\"${rand}\"," "${sdir}/config.cfg"
    else
        printf "%s_mode=\"%s\"" "${sfile}" "${rand}" >>"${sdir}/config.cfg"
    fi
else
    if egrep -q "${sfile}_finddir" "${sdir}/config.cfg"; then
        finddir=$(egrep "${sfile}_finddir" "${sdir}/config.cfg" | egrep -o "\".+\"" | sed 's/\"//g')
    else
        finddir=${d_finddir}
    fi

    if egrep -q "${sfile}_title" "${sdir}/config.cfg"; then
        title=$(egrep "${sfile}_title" "${sdir}/config.cfg" | egrep -o "\".+\"" | sed 's/\"//g')
    else
        title=${d_title}
    fi

    if egrep -q "${sfile}_desc" "${sdir}/config.cfg"; then
        desc=$(egrep "${sfile}_desc" "${sdir}/config.cfg" | egrep -o "\".+\"" | sed 's/\"//g')
    else
        desc=${d_desc}
    fi

    if egrep -q "${sfile}_mode" "${sdir}/config.cfg"; then
        rand=$(egrep "${sfile}_mode" "${sdir}/config.cfg" | egrep -o "\".+\"" | sed 's/\"//g')
    else
        rand=${d_rand}
    fi
fi