Score:0

How do I get a bash script file to request a prompt before continuing to the next step?

cn flag

I have a one line script to create a file in a directory and open the file with Kwrite. I'd like the script to prompt for the file name to be created and to be opened by Kwrite. At present it doesn't even open the terminal but ideally it should. How do I achieve this with the following script?:

cat > file.php & kwrite file.php

My intention is to paste copied portions of one file to create a new file.

vanadium avatar
cn flag
Better ask about what you want to achieve, rather than about help with a method you *think* will allow you to achieve your goal. For what you try here, you do not need a script. A simple `kwrite filename` will immediately open kwrite and have that save to `filename`.
Mark Lee avatar
cn flag
@vanadium what you say makes much sense. I'm converting some html layouts to WordPress php templates and am trying to reduce some repetition. A WP page comprises several included blocks. Each block of html markup needs to be integrated with some php. After I copy the block I want to make a php file without having to have multiple screens and applications. I open a new php file with. `printf '<?php get-header()?>\n\n<?php get-footer()?>' > foo.php & kwrite foo.php` works but I want to be prompted to name each new file.
Score:3
cn flag

Option 1: Using a bash script parameter

Create the script like so:

#!/bin/bash
( echo something > "$1".php && kwrite "$1".php ) &
clear

Then, you can call the script passing the file name after the command. The script will then automatically create the file with the content you wish, adding the .php extension automatically, and open the file in kwrite, while releasing the terminal prompt for you to enter a following command.

$1 in the script is a variable that automatically retrieves the next word on the command line. e.g. if the script is called phpfile, then the command

phpfile myfile

will create and open the file myfile.php.

Option 2. Using the read command

The read command allows to prompt the user for input at the command line.

#!/bin/bash
echo "Please enter filename: "
read FILENAME
echo something > "$FILENAME".php && nohup kwrite "$FILENAME".php &
clear

nohup may be useful if you are working from the terminal. It detaches the editor from the terminal processes, so it will not be closed if you close the terminal.

Option 3. Using a graphical tool

Zenity, installed by default in Ubuntu, or Kdialog on the Plasma desktop, allow you to prompt the user for input in a graphical dialog for use in scripts. You invoke these tools in a script. The user input then is placed in a variable. Advantage here is that then you could assign the script to a shortcut key. Then you could summon it with a single keypress to have the Zenity dialog pop up and have the script do its work.

#!/bin/bash
FILENAME=$(zenity --entry --title "Name request" --text "Please enter file name:")
echo something > "$FILENAME".php && nohup kwrite "$FILENAME".php &
clear
Mark Lee avatar
cn flag
Thanks @vanadium. You set me on the path to the solution with the tokenization. Your answer creates the empty file(s). This script prompts for a name: `#!/bin/bash echo "Please enter filename" read filename printf '<?php get_header()?>\n\n<?php get_footer()?>' > "$filename".php & kwrite "$filename".php` . My issue now is to execute the script via a double click. I'll install Zenity presently because it sounds from what you said exactly the solution.
vanadium avatar
cn flag
Indeed, zenity will probably be what you are looking for. For completeness, I added also the "read" option (input on the terminal), and I added examples for all the options. M
Mark Lee avatar
cn flag
your recommendation of Zenity led me to Kdialog and opened up a whole new aspect to my script that has grown from seven lines to 27 with a radio list box that opens up options for the user (me). It also made error handling easier for a novice. Thanks again for your help.
vanadium avatar
cn flag
Please accept the answer if it is useful for you: mark the checkbox next to the question. I have also made reference to Kdialog for Plasma users in my answer.
Mark Lee avatar
cn flag
Done! Thought I had already accepted it but seems I had only up voted. Good that you added Kdialog as it's installed with KDE. I've even done a README.md now that this looks so nifty.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.