0

I want to open a console (Konsole) to (1) request the user enters a filename, (2) create a file with basic content and (3) open the file in a text editor (Kwrite).

If I run the below in file.sh, using "Run in Konsole", it prompts for input, prints the content and opens the new file in Kwrite, all as expected:

#!/bin/bash
echo -n "Enter filename and press [ENTER]: "
read filename
printf '<?php get_header()?>\n\n<?php get_footer()?>' > "$filename".php & kwrite "$filename".php

However when I execute the below version of the script by double clicking on the file to open a console, execution stops after I enter the file name and hit enter.

#!/bin/bash
konsole --hold -e echo -n "Enter filename and press [ENTER]: "
read filename
printf '<?php get_header()?>\n\n<?php get_footer()?>' > "$filename".php & kwrite "$filename".php

Where am I going wrong? It seems "read" is not recognised as a command in the second version when the script opens Konsole.

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
Mark Lee
  • 163
  • 11
  • Do you need to specify `konsole --hold -e` before each command? – StarshipladDev Jun 08 '21 at 01:12
  • I'm not familiar with konsole, but it looks to me like only the `echo` command will run in your konsole window; the other two commands will run as though you hadn't use the `konsole` command... because you didn't use it on them. On the other hand, if you put `konsole --hold -e` before each command, I think that'll open 3 different konsole windows, one for each command (and they won't share variables, etc). I suspect you have to use something like `konsole --hold -e "sh -c 'entire script goes here'"` (and the quoting'll be really weird). – Gordon Davisson Jun 08 '21 at 04:00
  • 1
    Also, it looks to me like the `&` in that last line will cause a race condition because it tries to run both commands at once. If `kwrite` manages to open the file before `printf` is done writing to it, you'll get something like a blank file. Just make them separate commands. – Gordon Davisson Jun 08 '21 at 04:01
  • @MarkLee: What exactly does _double clicking on the file_ mean? What file are you talking about, and what has "double clicking" to do with a bash-script? – user1934428 Jun 08 '21 at 08:59
  • @user1934428 if you read the question you'd see mention of a .sh file. They can be made executable by setting permissions on them to do so. – Mark Lee Jun 08 '21 at 13:53
  • @GordonDavisson, based on your comment a two file solution is what I've arrived at until I can attempt a maze of quotation marks: ```#!/bin/bash konsole --hold -e bash includes.sh``` – Mark Lee Jun 08 '21 at 14:41
  • @MarkLee : I know that you want to run a command. I just don't know where you "double click". If I run a command, I type the command name (perhaps including the directory path) and hit the enter key. – user1934428 Jun 09 '21 at 06:39
  • @user1934428 you can write a command in a file and execute it by clicking on the file if permission is set on it as executable. If you read the question, I was asking about working with a file, not typing at the prompt. Here is exactly what I wrote: "However when I execute the below version of the script by *double clicking on the file* to open a console, execution stops after I enter the file name and hit enter." That followed "*I run the below in file.sh*" earlier in the post. – Mark Lee Jun 09 '21 at 22:25
  • @MarkLee : In this case, doesn't the behaviour depend on the operating system and window manager, and which program is used to present the file as "clickable"? For instance, there may be a difference when I am using MacOS and `Finder` to show the files to start, or when I am using Linux with KDE and `Dolphin` to show the files. My guess is that you are using KDE, since you wrote _konsole_, but IMO this information should go into the question. – user1934428 Jun 10 '21 at 06:03

0 Answers0