1

Trying to get bash to combine read and sed to insert a variable into a script. Doesn't necessarily have to be sed but something along those lines.

Something like so:

INTERNAL="`read -p "PATH " path` | sed -i '3i\$INTERNAL="/run/media/blah" blah.sh

I could also implement python as well. My project is written in shell but I could use a setup.py script for the initial run.

I'm trying to make a smart script to setup variables during the first run and save them for future usage. In this case it's a path to an external/internal device to backup $HOME. I am going to make the script search for a file: $HOME/.already_ran.txt(hidden dummy file for the script to search for to see if it's been ran before) If it doesn't find that file, it will prompt the user to enter the variables, in this case the paths. I don't want to have them have to open up the script manually and add the variable.

2
  • Previously unix.stackexchange.com/q/557795/5132 .
    – JdeBP
    Dec 21 '19 at 4:09
  • When the user runs the program the second time do you want the program to prompt for the variables again, but with defaults set to the values they entered the first time or do you want to skip asking anything at all?
    – icarus
    Dec 21 '19 at 4:35
0
#!/bin/bash

conffile=$HOME/.myscript.env

if [[ -f $conffile ]]; then
    if ! source "$conffile"; then
        printf 'Error parsing configuration file "%s"\n' "$conffile"
        exit 1
    fi
else
    read -p 'Please enter device path: ' devpath

    # further steps to verify correctness of $devpath here
    # then...

    declare -p devpath >"$conffile"
fi

# the rest of the script using "$devpath" goes here

declare -p varname will output a bash statement that defines the variable varname with its current value. Writing this to a file gives you a file that you can source to recreate the variable.

For a simple shell variable, devpath, containing some pathname, /some/path, what is written to $conffile might be something like

declare -- devpath="/some/path"

The if-statement wrapping the source command ensures that if someone has tampered with the syntax of the saved file to make the shell unable to read it, the script will terminate.

declare -p may also be used to save environment variables and array variables etc. to file for later reconstruction.

2
  • Awesome. I will try that out. I've not used the declare yet. I've just been trying with sed and read. Will this permanently add the variable INTERNAL="/path/to/destination" \n EXTERNAL="/path/to/destination" ? I also want to implement a custom path variable. If this doesn't work, I'm debating using df -hx tmpfs --output=source,target with a select loop. I think that might be better anyway. It'll show the mounted devices so they're not stuck to just those two paths. I'll upload the script to Github so you guys can see. I'll drop the link when I do. Dec 22 '19 at 23:23
  • @alexProvencher If you use declare -p INTERNAL EXTERNAL >somefile, then two declare commands are written to the file somefile. If you later source that file, those commands will be executed and this creates the variables with the exact data that they had when you ran the declare -p command with them.
    – Kusalananda
    Dec 22 '19 at 23:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.