In bash, read to a variable with a default value to edit


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting In bash, read to a variable with a default value to edit
# 8  
Old 04-28-2009
Method of real-time editing of variables in Bash

I realize this is an old post, but since I was so frustrated when I was trying to figure out how to do this, I thought it might be valuable to someone else to see this answer to the problem.

This may be pretty ugly to sophisticated programmers. I'm very "wet behind the ears" in Bash, or anything else for that matter. By the way, this is done in a Debian etch environment.

The idea is to save the variable to a file, and then open the file with an editor, make the desired changes, save the result, and exit the editor.

There must be some shell gymnastics going on here that I don't understand, because until I specified the whole path (with pwd), the edit did not take place.

If you want the new variable to exist as a permanent part of your script, you'll have to figure that out yourself. In my (DAR backup) program, I'm saving my editable variables to external files.

Here's my example script. The variable begins with 3 errors to be edited. So you can test it 3 times before you get it right. At the end the temporary file is deleted, because by that time you will have placed the variable where you want it.:

Code:
#!/bin/bash

## To test the editing of a variable from inside a script
if [ -f `pwd`/editvarfile.txt ] ; then echo "" ; else echo "This is a variible ti be editied." > `pwd`/editvarfile.txt ; fi
editvar=$(cat editvarfile.txt)
pickans="n"
while [ "$pickans" = "n" ] ; do
echo -n "The present variable is "$editvar".  Is this correct?  (y/n) "
read pickans
if [ "$pickans" = "n" -o "$pickans" = "N" ] ; then
echo ${editvar} > editvarfile.txt
kwrite editvarfile.txt
editvar=$(cat editvarfile.txt)
fi
done
rm editvarfile.txt

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Using read to assign value to bash variable not working

Hi, I am attempting to assign the output of the following command, to two bash variables, var1 and var2 using "read," but it doesn't seem to be working. # openstack hypervisor stats show | awk -F'|' 'NR==14{print $2,$3}' vcpus 92 # echo $? 0 # openstack hypervisor... (4 Replies)
Discussion started by: sand1234
4 Replies

2. UNIX for Advanced & Expert Users

[BASH] Read value of variable, but not comment on the same line

Heyas So while i'm trying to increase security/usability of my TUI commands, i'm currently at tui-conf-get. There was also a bug, i wanted to fix, but sadly that bugfix is more insecure - but twice as fast as my current buggy method. I've added a getopts toggle, '-s' to toggle beSecure,... (8 Replies)
Discussion started by: sea
8 Replies

3. Shell Programming and Scripting

[Bash] Read History function & Read Arrowkeys

Hi. How can I create a history function? (By "read" command or so) & How can I configure a read command so that the arrow keys are not displayed so funny? (^[[A) Thanks in advance. (4 Replies)
Discussion started by: sinnlosername
4 Replies

4. Shell Programming and Scripting

scan and edit in bash

so assume I have a dozen files in local directory and half of them are .txt and I only want to scan these text files and go inside each of them and replace absolute paths (e.g. C:\blabla\more blahblah\myfile.txt) with just the name of that file (myfile.txt) and then go to next line and look if... (6 Replies)
Discussion started by: Jaymz
6 Replies

5. Shell Programming and Scripting

Help with Bash piped while-read and a read user input at the same time

Hi I am new to writing script and want to use a Bash Piped while-read and read from user input. if something happens on server.log then do while loop or if something happend on user input then do while loop. Pseudocode something like: tail -n 3 -f server.log | while read serverline || read... (8 Replies)
Discussion started by: MyMorris
8 Replies

6. Shell Programming and Scripting

Edit csv file with bash

I' trying to use bash to edit a csv file from this format: "apples","oranges","grapes" "bread","butter","milk" To this: "apples oranges grapes" So that if I would open the csv file in open office, "apples oranges grapes" Would be in one single cell, and "bread (2 Replies)
Discussion started by: locoroco
2 Replies

7. Shell Programming and Scripting

Read and edit multiple files using a while loop

Hi all, I would like to simply read a file which lists a number of pathnames and files, then search and replace key strings using a few vi commands: :1,$s/search_str/replace_str/g<return> but I am not sure how to automate the <return> of these vis commands when I am putting this in a... (8 Replies)
Discussion started by: cyberfrog
8 Replies

8. UNIX Desktop Questions & Answers

grub, ok, bash, edit, kmdb and what else?

Hello There is a > prompt at Grub, # prompt for the console and $ for bash, but I am clueless about when and how to get into a specific prompt, how to move around between one prompt to another and how to exit. Is there a very basic guide anywhere that CLEARLY explains the type of shell... (2 Replies)
Discussion started by: mani1413
2 Replies

9. Red Hat

how to edit the bash shell?

Hello friends, I want to add some features to the bash shell.I logged in as the root.Even then could not view the source code of bash shell in RH 9,Fedora 8.It is in encrypted form.Can you please tell me how to include my code into bash shell. Please give me a sample code so that I can understand... (3 Replies)
Discussion started by: nsharath
3 Replies

10. Linux

How to edit /etc/fstab when root was mounted as read only

can somebody help me out in editing the /etc/fstab. I am on RHEL5 (Tikanga). **The problem is that i have given a wrong LABEL in /etc/fstab for root volume and so after reboot, it is unable to resolve the wrong LABEL; so, i have to edit the /etc/fstab :-( -ilan (3 Replies)
Discussion started by: ilan
3 Replies
Login or Register to Ask a Question