Interactive script to give user option of what text to add


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Interactive script to give user option of what text to add
# 1  
Old 05-18-2011
Question Interactive script to give user option of what text to add

Hello all,

I have created a script that will remove the first two lines of one text file (body.txt) and then add the text from a different text file (header.txt) directly to the beginning of the freshly modified body.txt file. It is as follows:

Code:
#!/bin/bash

## This script will add a header to the text file generated from the ChemCraft program.  
## Make sure you have modified the header template files to your specific needs!

sed '1,2d' body.txt >tmp && mv tmp body.txt
cat /home/$USER/header.txt body.txt >tmp && mv tmp body.txt

I want to add the option to this script of letting the user choose which header to add. Let us say that there are four possible headers that one could add i.e. head1.txt, head2.txt, head3.txt and head4.txt and that these files are all located in /home/$USER/.

How would I go about modifying my script in such a way to so that the user would be prompted to chose one of these i.e., when they run the script, they get and must choose either the number 1, 2, 3 or 4.

Code:
Please chose the header you want to add:
1. head1.txt
2. head2.txt 
3. head3.txt
4. head4.txt

Once the user has chosen the number, then the script runs and uses the one that they chose, rather than the specific one I have in my file. I would also like it if the only values the numbers 1 through 4 would be accepted.

thanks
# 2  
Old 05-18-2011
Try this

Code:
#!/bin/bash
echo " Please chose the header you want to add:
1. head1.txt
2. head2.txt 
3. head3.txt
4. head4.txt"
read choice
sed '1,2d' body.txt >tmp && mv tmp body.txt
cat /home/$USER/head$choice.txt body.txt >tmp && mv tmp body.txt

If your sed version supports "-i" option, then you can use the sed command like this sed -i '1,2d' body.txt. This will modify the file directly.

regards,
Ahamed
This User Gave Thanks to ahamed101 For This Post:
# 3  
Old 05-18-2011
That works. Although there is about a 10 second pause before the script pops up the menu.

Thanks very much!

Quote:
Originally Posted by ahamed101
Try this

Code:
#!/bin/bash
echo " Please chose the header you want to add:
1. head1.txt
2. head2.txt 
3. head3.txt
4. head4.txt"
read choice
sed '1,2d' body.txt >tmp && mv tmp body.txt
cat /home/$USER/head$choice.txt body.txt >tmp && mv tmp body.txt

If your sed version supports "-i" option, then you can use the sed command like this sed -i '1,2d' body.txt. This will modify the file directly.

regards,
Ahamed
---------- Post updated at 02:11 PM ---------- Previous update was at 02:00 PM ----------

My next question is what if the menu options are NOT the names of the actual files?

That is, the menu option is a descriptive name and the number that the user presses determines which file it should use.

How does one pass the variable to the cat command?

Instead of:
Code:
1. head1.txt
2. head2.txt 
3. head3.txt
4. head4.txt

I have
Code:
1. Optimization with ZMAT
2. Optimation with Cartesian
3. Frequency
4. Transition State Search

where
1. Optimization with ZMAT corresponds to the file name "opt_zmat_head.txt"
2. Optimation with Cartesian corresponds to the file name "opt_cart_head.txt"
3. Frequency corresponds to the file name "freq_head.txt"
4. Transition State Search corresponds to the file name "TS_search.txt"

All of these files would be located in /home/$USER/.

---------- Post updated at 03:15 PM ---------- Previous update was at 02:11 PM ----------

Answered my own question...thanks to all who helped out.

Code:
#!/bin/bash

# Script to add input headers to GAMESS(US) input files generated by ChemCraft.
# This script assumes that there are 4 headers that you can use:
# opt.zmat.txt, opt_cart.txt, freq.txt and TS_Search.txt
# These files should be placed in /home/$USER directory and modified to your liking.

echo " Please chose the header you want to add:
1. Optimization with ZMAT
2. Optimation with Cartesian
3. Frequency
4. Transition State Search"
read choice
            if [ "$choice" = "1" ]; then
               head='opt_zmat'
            
            elif [ "$choice" = "2" ]; then
               head='opt_cart'
            fi

            if [ "$choice" = "3" ]; then
               head='freq'
            fi

            if [ "$choice" = "4" ]; then
               head='TS_Search'
            fi
sed -i '1,2d' $in.inp
cat /home/$USER/$head.txt $in.inp >tmp && mv tmp $in.inp

Quote:
Originally Posted by marcozd
That works. Although there is about a 10 second pause before the script pops up the menu.

Thanks very much!



---------- Post updated at 02:11 PM ---------- Previous update was at 02:00 PM ----------

My next question is what if the menu options are NOT the names of the actual files?

That is, the menu option is a descriptive name and the number that the user presses determines which file it should use.

How does one pass the variable to the cat command?

Instead of:
Code:
1. head1.txt
2. head2.txt 
3. head3.txt
4. head4.txt

I have
Code:
1. Optimization with ZMAT
2. Optimation with Cartesian
3. Frequency
4. Transition State Search

where
1. Optimization with ZMAT corresponds to the file name "opt_zmat_head.txt"
2. Optimation with Cartesian corresponds to the file name "opt_cart_head.txt"
3. Frequency corresponds to the file name "freq_head.txt"
4. Transition State Search corresponds to the file name "TS_search.txt"

All of these files would be located in /home/$USER/.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Recording user input from interactive shell script

Hello, I want to start out by saying that I am fairly new to scripting and am looking for someone that can point me in the right direction. Basically what I need is a way to run a interactive script that will prompt users with questions weather that be yes/no or a specific answer.. I want to be... (3 Replies)
Discussion started by: shoutcast
3 Replies

2. Shell Programming and Scripting

Script to call a menu script and redirect each option to a text file

Hello, I want to design a script that will call an existing menu script and select options one by one and redirict the out put to a file. For example;- In the script MENU.sh there are 10 options i want to design a script MENU2.sh that will select option 2 3 4 6 7 10 and redirict the output... (4 Replies)
Discussion started by: spradha
4 Replies

3. Shell Programming and Scripting

Script to give a user sudo permissions

Can some one please let me know a script which gives the user sudo permissions? Thanks in advance.... (6 Replies)
Discussion started by: Revanth547
6 Replies

4. Shell Programming and Scripting

Interactive shell - Give answers to shell

Hi, I have a script that calles some other scripts. Those scripts are expecting some inputs. Right no I am typing manually. But now the number of questions from other scripts are too much and I want to give asnwers autimatically. example. Other scripts gives me 2 options 1) joom... (2 Replies)
Discussion started by: microsim
2 Replies

5. Homework & Coursework Questions

How to write script that behaves both in interactive and non interactive mode

Q. Write a script that behaves both in interactive and non interactive mode. When no arguments are supplied it picks up each C program from the directory and prints first 10 lines. It then prompts for deletion of the file. If user supplies arguments with the script , then it works on those files... (8 Replies)
Discussion started by: rits
8 Replies

6. Homework & Coursework Questions

Help with Interactive / Non Interactive Shell script

Q. Write a script that behaves both in interactive and non interactive mode. When no arguments are supplied it picks up each C program from the directory and prints first 10 lines. It then prompts for deletion of the file. If user supplies arguments with the script , then it works on those files... (1 Reply)
Discussion started by: rits
1 Replies

7. Shell Programming and Scripting

Non-interactive user switch to root

Is is possible switch user from a non-root user to root user without entering the password interactively inside a korn shell script which is run by a non-root user? e.g. I have a non-root user called infodba who is in dba group and I want to create a shell script which is executed by infodba... (5 Replies)
Discussion started by: stevefox
5 Replies

8. Shell Programming and Scripting

How to hide user inputted text for interactive unix shell script?

Hi everybody, Do you know how to hide the text for interactive unix shell script? Just like the case for inputting password during logon. Patrick (1 Reply)
Discussion started by: patrickpang
1 Replies

9. Shell Programming and Scripting

SFTP- Non-interactive user authentication

Hi All, sftp -b script.txt <hostname> user-authentication through non-interactive way is desired. But, its failing to do so. Could anyone kindly advise. Thanks for any/all help at the earliest. Regards, Dheeraj. (1 Reply)
Discussion started by: dheeruchakri
1 Replies

10. UNIX for Dummies Questions & Answers

How to give permission for a specified user

Hi All, How can i give permission for a specific user ( eg. admin ) ? I tried with chmod admin+r prog.sh which doesnt work. Is there any way i can specify a user's name and give the permission? Thanks in advance. Saneesh Joseph. (1 Reply)
Discussion started by: saneeshjose
1 Replies
Login or Register to Ask a Question