0

I'm working on a small bash script, and it essentially will just do two things. Execute two update commands together, and list keybinds. In the same way, for example, chmod +x would work, using +x as a variable. The way I'd like it is if I entered bash filename.sh -Y, I would get the keybinds I needed, and if I replaced -Y with -X, I would have my two update commands running. How would I do this?

Edit; I forgot to mention this, but it honestly doesn't matter if the variable has a hyphen in it. What I mean by the title of the post is, how do I use the -Y in bash filename.sh -Y without having to hit enter first? Here's the script I'm currently using.

#!/bin/sh

read var

if [ var == upd ]
then 
    sudo pacman -Syyu; yay -Syu
fi

This works perfectly fine when I input upd after pressing enter after running bash filename.sh. How would I get the same input with bash filename.sh upd?

4
  • Have you heard of getopts?
    – FelixJN
    Sep 4, 2021 at 15:43
  • Reading up on that right now
    – calimari
    Sep 4, 2021 at 16:17
  • Eh, which one do you want? To use the command line arguments, like the +x in chmod +x, meaning the user would run bash script.sh whatever<enter> (or preferably just script.sh whatever<enter> after you set the execute permission and hashbang correctly, and place the script in $PATH). Or do you want to read input from the user after the script starts, in which case the user would run something like script.sh<enter>whatever<enter> (or if you want to read just a single keystrike without enter, you can do that too)
    – ilkkachu
    Sep 4, 2021 at 16:31
  • The first one, yes. I've tried to search for the answer, but haven't really found anything much.
    – calimari
    Sep 4, 2021 at 16:41

1 Answer 1

-1

You should use the command-line argument variables $1, $2, $3, etc. $1 is automatically set to the first word after the command on the command line. $2 is the second word. $3 is the third, etc. You can also use the variable $#, which contains the number of arguments on the command line, to see if there are any arguments.

So your script would be:

#!/bin/bash

if [ $# -gt 0 ]; then
    var="$1"
else
    read var
fi

if [ "$var" == upd ]; then 
    sudo pacman -Syyu; yay -Syu
fi

Note: You should ALWAYS put quotation marks around any variable that could contain spaces, especially if the variable might come from user input.

10
  • LinuxSecurityFreak: Your edits were unnecessary. '$#' is guaranteed to be an integer and therefore cannot be blank and cannot contain spaces. Also '==' is valid syntax and it helps avoid confusion between comparison and assignment. Sep 4, 2021 at 17:13
  • Use at least shellcheck: ^-- SC3014: In POSIX sh, == in place of = is undefined. Sep 4, 2021 at 17:15
  • 1
    @AndrewVickers, == within [ .. ] isn't standard shell language, even if it's supported in Bash and ksh. It doesn't work e.g. in Dash, which is /bin/sh on Debian and Ubuntu (can't remember about the shells on other OS's). It also doesn't work on zsh, since =foo has another meaning there. For $#, you'd think it would be safe, since the shell is supposed to reset IFS on startup, but some shells don't (at least old versions of Dash and Busybox). And if IFS is set to digits, the integer in $# will get wordsplit.
    – ilkkachu
    Sep 4, 2021 at 17:21
  • Look, we are here to help, no one knows everything, we are not trying to damage your code, we're doing the opposite, mate. Rolling back our edits does mean you are not exactly perfect syntactically, but that's ok. | -1 Sep 4, 2021 at 17:24
  • 1
    @AndrewVickers, those esoteric issues come up from time to time on questions here when people think Bash is the only shell in the world, and run sh somescript instead of checking what the requirements actually are... I'm not sure it's helpful to propagate non-portable practices, like the == here, which has absolutely no upside compared to the more widely supported =. I'm also not sure why you insist on quoting the RHS of var=$1, when that's one of the rare contexts where the value does not go through word splitting, and the quotes don't do anything.
    – ilkkachu
    Sep 4, 2021 at 17:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.