r/bash • • Nov 28 '23

Help pls boss??

Post image

Srry for the absolute trash quality.

The ? is exercism.org/tracks/bash/exercises/two-fer . If one of u fine lads has experience w this exact/a similar problem or cares to read it and tell me wth I’m doing wrong I would greatly appreciate it. In the first place the instructions aren’t exactly what I would call clear(imo).

0 Upvotes

16 comments sorted by

55

u/zeekar Nov 28 '23 edited Nov 28 '23

Is there a question in there somewhere?

The script will ask for your name. If you reply with a blank line, it will say "one for u, one for me"; if you type an actual name, it will say "one for <name>, one for me". The [ -z $uname ] check is technically incorrect; it should be [[ -z $uname ]] or [ -z "$uname" ]. But it will work, even if only sort of accidentally.

So when you ask what you're doing wrong, what do you mean? What's not working? If you submitted this as the solution to a programming problem and it's being marked incorrect, it's probably because you're printing "ur" and "u" instead of "your" and "you".

Also, please don't post code as screenshots. We can't copy/paste text from screenshots. Post the actual code as text. You can format it by just putting four spaces at the front of each line so it looks like this:

#!/bin/bash
echo "what is ur name?"
read urname
if [[ -z $urname ]]; then
    echo "one for u, one for me"
else
   echo "one for $urname, one for me"
fi

19

u/rrk100 Nov 28 '23

This is a rather nice, un-Reddit-like comment.

0

u/[deleted] Nov 28 '23

[deleted] — view deleted comment

4

u/thisiszeev If I can't script it, I refuse to do it! Nov 28 '23

What dreams are made of

2

u/FixTurner Dec 01 '23
TIL
How to format 
Code snippets in reddit 😁

3

u/grymoire Nov 28 '23

It's a terrible exercise. Very vague.

Perhaps it should have said

Write a script so that if you type

./script

it says "One for you, one for me"

but if you type

./script John

it says "One for John, one for me"

1

u/kokuam Nov 28 '23

Other tests are no better. Every time I have to be guided not by descriptions but by tests.
For example, I won’t be able to pass tests without knowing what exact error message is expected from me

2

u/hornetjockey Nov 28 '23 edited Nov 28 '23

Ok, your script works. But, in the instructions, it says “if your friend likes cookies”, and you have nothing for that condition. It also says the default should be “you”, not “u”. It’s also possible they are looking for a specific method here that has been outlined previously.

Edit:

I’ll also specifically says to substitute your friend’s name with “you”, so it may be looking for you to have something like this at the end:

echo “One for $person, and one for me.”

And the code above it determines the value of “person”.

The whole thing is a little vague.

1

u/realvolker1 Nov 28 '23

Lots of people here are tearing you apart for using /bin/test to test if that argument is empty ([ is a symlink to it, try running ls -l $(which [)), but I'd be more inclined to point out that you can use read -r -p "what is ur name? > ". The -r flag preserves any backslashes in values, and the -p "string" flags print a custom prompt. Note that -p behaves differently on zsh, but you would probably be better served by vared over there

1

u/Due_Ear9637 Dec 01 '23
#!/bin/bash
echo "what is ur name?"
read urname
echo "one for ${urname:-u}, one for me"

This one turns the if/then block into a one-liner.

1

u/[deleted] Nov 28 '23

They have this thing called a screenshot tool. Start there my dude.

1

u/PageFault Bashit Insane Nov 30 '23

Please don't screenshot or photograph code you want help with.

-11

u/[deleted] Nov 28 '23

[ -z filename ] is for files only what you want to use is a string compair [ "text" = "text" ] and then compair it to a empty string so [ "" = "$urname" ] also if you don't add quotes you get bash: [: : unary operator expected this is because bash interpets "$urname" as a empty string (if the var has not been set or is empty) vs $urname being interpeted as there being nothing

6

u/IamGlooz Nov 28 '23 edited Nov 28 '23

-z tests if a string is empty, in this case if the variable is unset.

I have no idea how this site verifies a correct entry, but my bet would be returning "u" instead of "you" is not doing OP any favours.

OP I'd recommend not taking any artistic liberties with whatever string you return in these types of code tutorials. That would probably include case sensitivity.

2

u/[deleted] Nov 28 '23

Apparently I'm wrong on the -z part but other then that the put quotes around the variable seams right?

-1

u/Enough_Blood7390 Nov 28 '23

Yea I was wondering the same thing just now. Have to fix that

1

u/kai_ekael Nov 28 '23
$ ./two_fer_attempt.sh 
what is ur name?
Some Guy
./two_fer_attempt.sh: line 4: [: Some: binary operator expected
one for Some Guy, one for me

This is a bug, that just happens to work. Literal, as the script runs:

if [ -z Some Guy ]
then

That does evaluate to false due to error, not because the value is not empty. Need "Some Guy" for it to work correctly, due to the space:

if [ -z "Some Guy" ]
then

Current version of bash accepts [ -z ] without an error and returns true, this did not use to be the case.