Ubuntu: Using source command to replace bash string with field array


I've programmed in other languages but bash and unix in general are very new to me. I think a bash wizard might see an obvious solution, using the source command to read in a settings file - and assign values to my command string?

Ideally I'd like to create a command string from all the field/value settings in a file, by replacing their matches in a string.

My file will look something like:


#!/bin/bash  #  # WiFi Settings  #  wifi_name=mywifiname  wifi_password=wifi-pwd  #  # Icecast Server Settings  #  icecast_server=icecast.domain.com  icecast_port=443  icecast_mount_url=/user/99999/show/0123456  icecast_show=RPi Demo Show  icecast_user=  icecast_password=icecast-pwd  #  # avconv setting for Raspbian Jessie Lite  # may not need if you're using a self compiled ffmpeg version  #  icecast_legacy=1  #  # Stream Settings - probably not safer to go higher unless great internet connection  #  stream_bitrate=128k  

So if I used command:

source settings.cfg  

There would be a variable named $stream_bitrate that would have the value of "128k".

I could then use a string replace on stream_parameters= <stream_bitrate> to make that look like stream_parameters= 128k

But I'm wondering if bash gurus can see a simple index based loop - or regular expression that might do the job for whatever parameters are loaded.

I can change my settings file to make it easier if needed. I've put "<>" around the replacement parameter locations - but those can be removed or modified if needed.

And it can be one command line string if needed.

EDIT: Move command line processing to second file

Would this work?


#!/bin/bash  #  # Load in config file settings  source settings.cfg    # build command line string  start_cmd=avconv -re -i test.mp3 -c:a libmp3lame -ac 2 -ar 44100 -content_type audio/mpeg -b:a  stream_parameters="$stream_bitrate -f mp3 -ice_name "$icecast_show" -password "icecast_password" -legacy_icecast $icecast_legacy"  icecast_setup=icecast://$icecast_user:$icecast_password@$icecast_server:$icecast_port$icecast_mount_url    stream_cmd = "$start_cmd $stream_parameters $icecast_setup"  


The script in your edit should work perfectly.

But you should note that Bash's variable declaration is picky. I rewrote your code:

# Strings with spaces must be quoted, or the shell will think the second word is a command (-re, in this case).  start_cmd="avconv -re -i test.mp3 -c:a libmp3lame -ac 2 -ar 44100 -content_type audio/mpeg -b:a"    # You can't nest quotes. A quote mark terminates the quotation, unless it's escaped.  stream_parameters="$stream_bitrate -f mp3 -ice_name $icecast_show -password $icecast_password -legacy_icecast $icecast_legacy"     # It's best to quote strings, as a habit.  icecast_setup="icecast://$icecast_user:$icecast_password@$icecast_server:$icecast_port$icecast_mount_url"    # Don't put spaces around the equals sign.  stream_cmd="$start_cmd $stream_parameters $icecast_setup"  

As for how to run a command defined by a string, it's super simple. Just run this: $stream_cmd

Also you might want to state the absolute path of the config file (maybe ~/settings.cfg), unless you want to use the relative location.

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »