Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Just last week I was asked to provide some possible screening questions to recruit for a Devops role. I figured this would be good:

"How do you pass a parameter to a Bash script so that the script will exit with an error if it's not passed?"

I ran this through ChatGPT and I could not get it to give ${1:?} or any variation of that syntax as an answer. So now I also have a way to filter candidates who are leaning on the LLM during the interview process.



That's an unwise way to filter out candidates. It's an obscure and unimportant syntax feature whose effect can be accomplished in many other, more readable ways. Writing line noise using obscure syntax is not a sign of a good programmer. If you want to find good candidates, give them an assignment in bash and make them explain their solution.


I am very proficient in Bash. My day job is as a Senior DevOps engineer. If I were doing an interview and was asked that question, I would never give the answer `${1:?}` (if I even remember at the time that it existed) because it is terrible code. It is obscure and unreadable.

The goal of a DevOps engineer should be to write maintainable code so that a junior can come in and tell what it does immediately.

As another commenter said, my answer would be: `[[ $# -ne 1 ]] && exit 1`. That's readable and understandable to anyone who has a basic understanding of bash scripting.


Is this for an in-person interview?

Because without checking `man` or stackoverflow (or leaning on ChatGTP) my first response would be to go with `if [ $# -lt 1 ]; ...`. This has the advantage of being pretty readable, and while I've been doing shell scripting for long enough to consider myself reasonably competent at it, I'd have to refer to the man page to be sure what `${1:?}` did.


I said "Give me the most succinct possible idiom" and it gave me the answer you were looking for: https://chat.openai.com/share/05a3d1aa-cd60-40a1-b6d5-ec0c9f...


The first answer is wrong, so is the second's explanation... twice it conflates unset with null/empty. Very illustrative!

The substance of the first answer:

    #!/bin/bash
    
    # Check if the first parameter is not provided
    if [ -z "$1" ]; then
        echo "Error: Parameter not provided."
        exit 1
    fi
The snippet precedes the true statement:

> This checks if the first parameter (`$1`) is empty.

But what happened to the supplied task? It was stated and echoed as:

> Anonymous: ...if it's not passed?

> ChatGPT: ...if a specific parameter is not passed...

> ChatGPT: ...if the parameter is set.

The second answer:

    #!/bin/bash
    : ${1?"Error: Parameter not provided"}
This is correct. But the explanation is not:

> ...and the `${1?...}` part checks if the first positional parameter (`$1`) is unset or null.

--

(the most succinct possible idiom is `#!/bin/bash -u`)


Thanks for the tip, I now know that I can’t use ChatGPT when I’m being interviewed with this question.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: