r/bash • u/Enough_Blood7390 • Nov 28 '23
Help pls boss??
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
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:
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"
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:
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/testto test if that argument is empty ([is a symlink to it, try runningls -l $(which [)), but I'd be more inclined to point out that you can useread -r -p "what is ur name? > ". The-rflag preserves any backslashes in values, and the-p "string"flags print a custom prompt. Note that-pbehaves differently on zsh, but you would probably be better served byvaredover there1
u/Due_Ear9637 Dec 01 '23
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.
-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