I have a bad problem and I hope you can give me some ideas! :)
What I want to do:
Simply read the user argument of the Script.
What is the problem:
special characters
Let me explain:
Scriptname: testscript
if the user types testscript -f filename.txt it prints out the name. Pretty easy.
But if the user types the following, it does not work for the shell: testscript -f file(somethings)name.txt
so if there are special characters, it always throws an sh: error....
Working: it works if the user writes this: testscript -f 'filename(something).txt'
with ' '
But this is inconvenient and often people forget that they have to write it.
Does anyone have an idea what I can do?
I thought about getting the argument and then add ' and ' at the beginning and end, I am not sure if the shell error is first..
Otherwise, does anyone have an idea!?
Would it be possible to check if there is an () somewhere in the filename before allowing userinput and remove () then?
Unfortunately, there's nothing your program can really do here - the error your users are seeing comes from the shell, before your program even has a chance to execute.
Related
How do we do Perl's '
$inp=<>;
to prompt user a single line, in Raku ?
As lines(); keep on prompting a user input so not work identically
Please help clarify
to prompt user a single line, in Raku ?
The prompt function does this.
my $input = prompt; # No message
my $name = prompt 'Name: '; # Or with a message
See get, which is an alias for lines(1), or something pretty close to that.
It initially surprised me how hard it is to discover in Raku resources -- doc, design doc, chat logs, etc. But I guess <> and line are so overloaded that it's not that odd after all.
I've added a comment about get's (lack of) discoverability in the doc issues queue. It would be helpful to those working on the doc if you added another comment in the same issue saying where you'd looked to get (!) an answer to your Q.
I've written an octave script, hello.m, which calls subfunc.m, and which takes a single input file, a command line argument, data.txt, which it loads with load(argv(){1}).
If I put all three files in the same directory, and call it like
./hello.m data.txt
then all is well.
But if I've got another data.txt in another directory, and I want to run my script on it, and I call
../helloscript/hello.m data.txt
this fails because hello.m can't find subfunc.m.
If I call
octave --path "../helloscript" ../helloscript/hello.m data.txt
then that seems to work fine.
The problem is that if I don't have a data.txt in the directory, then the script will pick up any data.txt that is lying around in ../helloscript.
This seems a bit fragile. Is there any way to tell octave, preferably in the script itself, to get subfunctions from the same directory as the script, but to get everything else relative to the current directory.
The best robust solution I can think of at the moment is to inline the subfunction in the script, which is a bit nasty.
Is there a good way to do this, or is it just a thorny problem that will cause occasional hard to find problems and can't be avoided?
Is this in fact just a general problem with scripting languages that I've just never noticed before? How does e.g. python deal with it?
It seems like there should be some sort of library-load-path that can be set without altering the data-load-path.
Adding all your subfunctions to your program file is not nasty at all. Why would you think so? It is perfectly normal to have function definitions in your script. The only language I know that does not do this is Matlab but that's just braindead.
The other alternative you have is to check that the input file argument, data.txt exists. Like so:
fpath = argv (){1};
[info, err, msg] = stat (fpath);
if (err)
error ("could not stat `%s' : %s", fpath, msg);
endif
## continue your script knowing the file exists
But really, I would recommend you to use both. Add your subfunctions in your main program, the only reason to have it on separate file is if you plan on sharing with other programs, and always check input arguments.
Okay, so I'm trying to make a program that "understands" user input and does what they tell it to do. People usually just use specific commands such as "open this file" and it only works if the user types EXACTLY that. I'm trying to give my users a little bit of leeway, so that they can type something like what they want to happen, and the computer will get the general idea. With that block of rambling aside, I've run into a problem.
set word%wordNum%=%word%
:fileExtension
set extChk= %letterNum% - 2 REM Includes the period of the extension
call set extension=%%_albaiRec:~%extChk%,4%%
::extChk is checking for a period so the program will recognize a file extension
set file=
That last line is where I get stuck...
I'm trying to use that last recorded word variable.
set var=7
set word7=Wanted text
echo %word%var%%
Sorta like that?
Add setLocal enableDelayedExpansion to the start of your script.
Then replace echo %word%var%% with echo !word%var%!.
For more information - http://ss64.com/nt/delayedexpansion.html
Before I remembered how to accomplish what I was doing, I tried a couple different things, kind of just hacking at it.
What I was trying to accomplish was to set the following string as a variable and then echo it out in a batch script:
<?php require('__php__.php'); ?>
I eventually worked it out with help from SO, but before I got there, I tried this (for some reason):
set (phpStr=<? php require('__php__.php'); ?>)
Which I realize doesn't make any sense. However, how the cmd shell interpreted what I wanted to do was as follows:
set (phpStr= php require('__php__.php'); ? 0<? 1>)
In other words, when I typed the code in the second code block above, and turned on echo in the script, what showed up in the cmd shell was the command in the third code block. Then there was a syntax error, and the script exited.
Can anyone explain what happened? (Not why it didn't work. That is obvious to me, but rather, how it arrived at the interpretation it did. It's a pretty awesome restructuring of the original command. I just can't figure out how it got there.)
You need to escape redirection and other poison characters with ^ or the redirection will be active and try to create files etc. % is a special case.
You can also use something like this:
#echo off
for /f "delims=" %%a in ("<?php require('__php__.php'); ?>") do echo %%a
I'm just switch to zsh and now adapting the alias in which was printing some text (in color) along with a command.
I have been trying to use the $fg array var, but there is a side effect, all the command is printed before being executed.
The same occur if i'm just testing a echo with a color code in the terminal:
echo $fg_bold[blue] "test"
]2;echo "test" test #the test is in the right color
Why the command print itself before to do what it's supposed to do ? (I precise this doesn't happen when just printing whithout any wariable command)
Have I to set a specific option to zsh, use echo with a special parameter to get ride of that?
Execute the command first (keep its output somewhere), and then issue echo. The easiest way I can think of doing that would be:
echo $fg[red] `ls`
Edit: Ok, so your trouble is some trash before the actual output of echo. You have some funny configuration that is causing you trouble.
What to do (other than inspecting your configuration):
start a shell with zsh -f (it will skip any configuration), and then re-try the echo command: autoload colors; colors; echo $fg_bold[red] foo (this should show you that the problem is in your configuration).
Most likely your configuration defines a precmd function that gets executed before every command (which is failing in some way). Try which precmd. If that is not defined, try echo $precmd_functions (precmd_functions is an array of functions that get executed before every command). Knowing which is the code being executed would help you search for it in your configuration (which I assume you just took from someone else).
If I had to guess, I'd say you are using oh-my-zsh without knowing exactly what you turned on (which is an endless source of troubles like this).
I don't replicate your issue, which I think indicates that it's either an option (that I've set), or it's a zsh version issue:
$ echo $fg_bold[red] test
test
Because I can't replicate it, I'm sure there's an option to stop it happening for you. I do not know what that option is (I'm using heavily modified oh-my-zsh, and still haven't finished learning what all the zsh options do or are).
My suggestions:
You could try using print:
$ print $fg_bold[red] test
test
The print builtin has many more options than echo (see man zshbuiltins).
You should also:
Check what version zsh you're using.
Check what options (setopt) are enabled.
Check your ~/.zshrc (and other loaded files) to see what, if any, options and functions are being run.
This question may suggest checking what TERM you're using, but reading your question it sounds like you're only seeing this behaviour (echoing of the command after entry) when you're using aliases...?