LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Editing a file within a script and then continueing (https://www.linuxquestions.org/questions/linux-newbie-8/editing-a-file-within-a-script-and-then-continueing-4175464082/)

SmurfGGM 05-30-2013 06:13 AM

Editing a file within a script and then continueing
 
Hi there,
Can someone please help out with this.

I am running a script and want to add the option of viewing and amending a file, then continue to the script.

If the user enters y, the file will be opened for editing. When editing is done, I want to continue the script.
Pressing n will continue the script,

I have ..

read -p "****** To change the file press y or n to continue" choice
echo ""
case "$choice" in
y|Y )
vi $statfile
;;
n|N ) echo "no";;
* ) ;;
esac

echo "contine script"

Many thanks

kaushalpatel1982 05-30-2013 06:29 AM

Find the bash file as below:
===============================
#!/bin/bash
trap "" 2 20
while :
do
clear
echo 1. open file
echo 2. exit
read option;

case $option in
1)
vi test.txt
;;
2)
exit
;;
esac
done
==================================
I hope this will help you.

SmurfGGM 05-30-2013 07:09 AM

Thanks for that.

After the filehas been opened for editing, I need to continue with the rest of the script.

How do I do that ?

Thanks

kaushalpatel1982 05-30-2013 07:20 AM

SmurfGGM,

I didnt get you. are you calling this script from other script?

What I was understand, When you complete with editing file, you should be back to menu.

SmurfGGM 05-30-2013 07:45 AM

When I finish editing the file that is called by the script, I want the script to continue running.

Thanks

shivaa 05-30-2013 07:50 AM

So where are you stuck? All seems ok.

Just try it, and let's know if you have any issue:
Code:

#!/bin/bash
read -p "Enter the filename to be edited: " file
read -p "To change the file press y or n to continue: " choice
case $choice in
y|Y)
vi $file
echo "File editing done" ;;
n|N) echo "File was not edited.";;
*) ;;
esac
echo "Continue script..."


SmurfGGM 05-30-2013 08:09 AM

Thanks

I get

read: -p: no coprocess
Continue script...

SmurfGGM 05-30-2013 08:27 AM

ah i got it working with ..


read resp1?"To change the file press y or n to continue: "
case $resp1 in
y|Y)
vi $statfile
echo "File editing done" ;;
n|N) echo "File was not edited.";;
*) ;;
esac
echo "Continue script..."

Thanks for your time

shivaa 05-30-2013 08:30 AM

Script is running fine on my system, so I guess you made some mistake. Once invoke the script with -xv option to see that where it goes wrong, as:
Code:

~$ bash -xv scriptname.sh
EDIT: You can even make it little simple, as:
Code:

#!/bin/bash
echo -n "Enter the filename to be edited: "; read file
echo -n "To change the file press y or n to continue: "; read choice
case $choice in
y|Y)
vi $file
echo "File editing done" ;;
n|N) echo "File was not edited.";;
*) ;;
esac
echo "Continue script..."



All times are GMT -5. The time now is 05:24 AM.