2

Is it possible to save last entered value of a variable by the user in the bash script itself so that I reuse value the next time while executing again?.

Eg:

#!/bin/bash
if [ -d "/opt/test" ]; then
echo "Enter path:"
read path
p=$path
else 
.....
........
fi

The above script is just a sample example I wanted to give(which may be wrong), is it possible if I want to save the value of p permanently in the script itself to so that I use it somewhere later in the script even when the script is re-executed?.

EDIT:

I am already using sed to overwrite the lines in the script while executing, this method works but this is not at all good practice as said. Replacing the lines in the same file as said in the below answer is much better than what I am using like the one below:

...
....
PATH=""; #This is line no 7
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"; 
name="$(basename "$(test -L "$0" && readlink "$0" || echo "$0")")";
...
if [ condition ]
fi
path=$path
sed -i '7s|.*|PATH='$path';|' $DIR/$name;
user465465
  • 441
  • 3
  • 13
  • 26

3 Answers3

3

Someting like this should do the asked stuff :

#!/bin/bash

ENTERED_PATH=""

if [ "$ENTERED_PATH" = "" ]; then
    echo "Enter path"
    read path
    ENTERED_PATH=$path

    sed -i 's/ENTERED_PATH=""/ENTERED_PATH='$path'/g' $0
fi

This script will ask user a path only if not previously ENTERED_PATH were defined, and store it directly into the current file with the sed line.

Maybe a safer way to do this, would be to write a config file somewhere with the data you want to save and source it . data.saved at the begining of your script.

Cédric Julien
  • 78,516
  • 15
  • 127
  • 132
3

In the script itself? Yes with sed but it's not advisable.

#!/bin/bash

test='0'

echo "test currently is: $test";

test=`expr $test + 1`
echo "changing test to: $test"

sed -i "s/test='[0-9]*'/test='$test'/" $0

Preferable method: Try saving the value in a seperate file you can easily do a

myvar=`cat varfile.txt`

And whatever was in the file is not in your variable.

I would suggest using the /tmp/ dir to store the file in.

Timothy Martens
  • 678
  • 4
  • 17
  • 3
    Might be good to mention some of the reasons it's not advisable. eg: permission issues, bad failure behavior (truncated script), violating principle of least surprise, doesn't work well for multiple-users, etc. – Laurence Gonsalves Dec 01 '11 at 17:23
  • @Laurence Gonsalves: +1 for pointing out the issues and probably I am facing the same problem with the one I was using(edited question) as it sometimes don't overwrite and sometimes get stuck,. – user465465 Dec 02 '11 at 01:40
1

Another option would be to save the value as an extended attribute attached to the script file. This has many of the same problems as editing the script's contents (permissions issues, weird for multiple users, etc) plus a few of its own (not supported on all filesystems...), but IMHO it's not quite as ugly as rewriting the script itself (a config file really is a better option).

I don't use Linux, but I think the relevant commands would be something like this:

path="$(getfattr --only-values -n "user.saved_path" "${BASH_SOURCE[0]}")"
if [[ -z "$path" ]]; then
    read -p "Enter path:" path
    setfattr -n "user.saved_path" -v "$path" "${BASH_SOURCE[0]}"
fi
Gordon Davisson
  • 118,432
  • 16
  • 123
  • 151
  • This is actually a very nice response, however not every distro/filesystem has extended attributes enabled or even fattr utilities to set these. There should be 'backup option' if one decides to use these. – Tomas Pruzina Jul 11 '13 at 12:50