I'm looking for passing a variable to script (I know how to do that), that will be echoed by the script in order to slightly modify that variable.
$ [scriptname] foo bar
where [scriptname] is the name of the program, and foo
and bar
the two arguments passed.
[Edited for clarification, after comment:] The resulting file should be named foo_bar1
, but with the option for the user to make minor modifications to this file name that one can't know beforehand.
Therefore it could look like
$ [scriptname] foo bar
The output file name will be foo_bar1
And here I wished I could interactively use, like backslash, remove the 1
and replace it by underscore (_
) and 2
:
The output file name will therefore be foo_bar_2
And this would subsequently be a known or new variable in the script, so that the results of this script can be written into an output file called foo_bar_2
.
Reason for my question is, that the file names are not just 3 chars, but rather long, and contain specific data. So the user ought not (try to) re-type dozens of characters; often the output file name will be the one as proposed. The user will see it, most often just 'Enter'. Though also have the chance to add own, minor, modifications. [I hope this explains somewhat better what I need.?]
Is this possible? If yes, I'd gladly learn how to do that.
And it isn't necessarily only the last number (I'd know how to remove/isolate it). But at times, I ought to be able to also call the output foofoo_bar_2
.
Based on my understanding of what you are trying to do, I have written a bash script. It takes two arguments. The script will exit if exactly two arguments are not provided. If the script finds two arguments it will combine them with an
_
in between and a1
as the suffix to form a "filename". Then the script will present this "filename" to the user and the user will have an opportunity to edit it as they please.The script:
Create a file called
altname
with the following contents:Save the file. Open a terminal and run the script with the command:
You should see a prompt like above. Use the arrow keys, delete, backspace, etc. to alter the name and hit Enter. The script will then display the final filename.
Hope this helps