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.
Option 1: Using a bash script parameter
Create the script like so:
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 calledphpfile
, then the commandwill create and open the file
myfile.php
.Option 2. Using the
read
commandThe
read
command allows to prompt the user for input at the command line.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.