Why does the code keep choosing the else option - cs50

so i'm in quite a bind with the ceaser problem set. I'm trying to confirm if the second command line argument is a number using the if-else statement below and the code keeps on choosing the 'else' option. I've converted the string argument with the 'atoi' function sooo help pls lolenter image description here

From the Microsoft C++ Docs:
By convention, argv[0] is the command with which the program is
invoked. argv[1] is the first command-line argument. The last
argument from the command line is argv[argc - 1], and argv[argc]
is always NULL.
The second command line argument would be argv[2] and your argc should equal 3.

Related

How to modify command line arguments inside bash script using set

I'm executing a shell script and passing few command line arguments to it.
I want to modify the arguments inside the script using set. Not all at once depending upon some conditions.
How can I do that?
Copy unmodified arguments at their respective location within set --
Say you want to modify value of argument 2:
set -- "${#::2}" 'new arg2 value' "${#:3}"
Explanation:
"${#::2}": Expands 2 arguments from index 0 (arguments 0 and 1)
new arg2 value: Becomes the value for argument 2.
"${#:3}": Expands all argument values starting at index 3.
Opinion:
Anyway, having mutable arguments is considered code-smell in modern programming. So I'd recommend you reconsider your approach to the problem you are trying to solve.

Python: why can a variable stand on its own?

I am a beginner in python told my friend today, that the following code would throw an error, but it did not:
a = 5
a
So I wondered, what does "a" actually do and why is the interpreter fine with this?
If this is a duplicate, please refer me to the right post and sorry in advance.
edit: I used a *.py file.
If you type this code into the shell and click enter, the value of a is returned. Functionally, as there is no operation being performed on a, the value of a will not change.
You define the variable in the line above. The variable contains a value, so the "NameError: name 'a' is not defined" error is not triggered.
Also, even if the variable is a different data type, for example, a string, the value of a is returned.
If you run the code in a different environment, the line won't be printed and the line won't impact the value of itself or of any other variables.
I think you tried it in der REPL Console, paste it to a *.py file and execute that. So when you just type the variable name and hit enter this is actually a print command behind the scenes
You can type in a int into the shell or whatever, and it will return it. The variable you put is just a int, so it returns 5.

How to test if a string variable in Robot Framework is empty?

How to test if a string variable in Robot Framework is empty?
My first naïve attempt looked like this:
Run Keyword If ${myVar}!=${EMPTY}
but it failed:
Evaluating expression '!=' failed: SyntaxError: unexpected EOF while parsing (, line 1)
I then found this issue at Github but it didn't suggest a solution, just that the error message was unclear. An alternative solution was presented here:
${length}= Get Length ${Portfolio_ste}
Run Keyword If ${length} Go To Edit Portfolio
but is this really the best practice?
(The context is that I use a variable argument list and if a certain variable contains a value something should be done, otherwise just ignore it)
The expression needs to be a valid python expression after variable substitution. Assuming for the moment that myVar might be something like the number 42, your expression would end up looking like this after substitution:
Run Keyword if 42!=
When comparing against the empty string you need to add quotes to guarantee that the expression is a proper python expression after substitution. For example:
Run Keyword If "${myVar}"!="${EMPTY}"
Try Get Variable Value. It solved my problem.

In BASH, can we assign and display the value in the variable _ (underscore)?

Answering to the following question:
Allowed characters in linux environment variable names #aiden-bell writes that the following patterns gives all allowed shell variable names in BASH : [a-zA-Z_]+[a-zA-Z0-9_]*
I found this to be true. In fact I can export value _="Just for fun". Unfortunately though, whenever I print it I get __bp_preexec_invoke_exec
I went through this thread and while it is instructive it doesn't actually answer my question. Irrespective of whatever the shell might do with the variable $_, can I use it for my own means? Also, whatever exactly is __bp_preexec_invoke_exec? Thanks and regards.
You can assign to the special parameter _, but the shell will also update its value after each command. Typically, you only use it as a dummy variable where you don't care about the result, such as in something like
while read _ second _ ; do ...; done < input.txt
where you only care about the second column of each input line.
From the man page:
_ At shell startup, set to the absolute pathname used to invoke
the shell or shell script being executed as passed in the envi-
ronment or argument list. Subsequently, expands to the last
argument to the previous command, after expansion. Also set to
the full pathname used to invoke each command executed and
placed in the environment exported to that command. When check-
ing mail, this parameter holds the name of the mail file cur-
rently being checked.

Long-form arguments aren't passed correctly when using std::process::Command

I have an issue where long-form arguments aren't passed correctly to a specific command line utility.
All of this works:
Command::new("mpg321").arg("--gain 100").arg("file.mp3").spawn().unwrap()
Command::new("mpg123").arg("-h 2").arg("file.mp3").spawn().unwrap()
mpg123 --halfspeed 2 file.mp3
But this:
Command::new("mpg123").arg("--halfspeed 2").arg("file.mp3").spawn().unwrap()
Returns:
mpg123: Unknown option "halfspeed 2"
I suspect that the fault is on the side of mpg123, but that doesn't make complete sense since it works when called from the command line.
--halfspeed 2 is two arguments. You should call arg twice:
Command::new("mpg123").arg("--halfspeed").arg("2").arg("file.mp3")...
or pass all arguments together as an array using .args:
Command::new("mpg123").args(&["--halfspeed", "2", "file.mp3"])...
mpg123 expects to be called like this: mpg123 --halfspeed 2 file.mp3, which makes argv look like {"mpg123", "--halfspeed", "2", "file.mp3"}. Your way of calling it is equivalent to mpg123 "--halfspeed 3" file.mp3, which makes argv be {"mpg123", "--halfspeed 2", "file.mp3"}, which mpg123 doesn't understand.
The reason that "-h 2" works is that single letter options can be used without a space (meaning you could just write -h2), so for those the argument parser needs to be able to handle the case that the option's letter and value are in the same argv entry. Apparently that logic is implemented in such a way that it works even if the argv entry contains a space.

Resources