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:

#!/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.

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

Try this

#!/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

1 Like

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:

1. head1.txt
2. head2.txt 
3. head3.txt
4. head4.txt

I have

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.

#!/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