0

I have the following bash script:

export USER_AT_HOST="<user@ipaddress>"
export PUBKEYPATH="$HOME/.ssh/<pubkeyfilename>"
ssh-copy-id -i "$PUBKEYPATH" "$USER_AT_HOST" -p 8524

I would like to modify the script, so that <pubkeyfilename> (which is currently hardcoded in the script), becomes a named input argument, and the same for the port number. Ideally, the script should do the following

  1. take two named arguments, -p/--port and --pubkeyfile, respectively an integer and a path
  2. perform input validation, i.e., check that -p/--port is an integer, if not empty, and that --pubkeyfile is a string. Of course, if this is not possible or too complex in bash, you can ignore this request.
  3. if an argument is not passed, use a default value
  4. if a wrong argument or the argument -h/--help is passed, an help message is displayed

This answer is highly regarded:

https://stackoverflow.com/a/14203146

but it doesn't show how to use default values if an argument is not passed, it doesn't perform input validation and it doesn't include an help method. Also, for the getopts option (which wouldn't allow me to use long option names), it refers to this tutorial

https://wiki.bash-hackers.org/howto/getopts_tutorial

which however gives me the error

This site can’t provide a secure connection
wiki.bash-hackers.org sent an invalid response.
ERR_SSL_PROTOCOL_ERROR

EDIT: this question is not a duplicate of How can I use long options with the Bash getopts builtin?, because the answers there either don't show how to use getops for long options (see https://stackoverflow.com/a/12026302/1711271) or don't show how to pass long options which require an argument (https://stackoverflow.com/a/30026641/1711271), or don't show how to set a default value for an option which is not passed (all of them). My OS is MacOS.

EDIT 2: my shell is bash:

> ps -p $$
  PID TTY           TIME CMD
67809 ttys001    0:00.01 -bash

I'm not going to learn another shell just for the sake of a simple script. However, if I understand correctly, it's possible to write a shell script that runs another shell and then terminate. Thus a solution based on zsh, while not ideal, could be acceptable.

DeltaIV
  • 4,773
  • 12
  • 39
  • 86
  • Aside from the page I linked to as duplicate, do you really **have** to use bash? If zsh would also be fine for you, you would have `zparseopt` available, which is handling long options nicely. – user1934428 Dec 08 '21 at 12:52
  • You did not say which OS you are using. Perhaps you have the _enhanced_ getopt (See _man getopt_), which using `-l`, supports long options. – user1934428 Dec 08 '21 at 12:56
  • @user1934428 1) the accepted answer clearly doesn't answer my question. This answer https://stackoverflow.com/a/30026641/1711271 doesn't handle input parameters. And no answer provides default values if a parameters isn't passed, so...no. 2). I do have to use bash. It's the shell I have, and it's also the only shell I know how to use. 3) OS is OSX. I'm editing the question to show this – DeltaIV Dec 08 '21 at 13:20
  • @user1934428 on second thoughts, I think it would be possible to use another shell just for the sake of running a single script (I only need to edit the shebang, right?). *However*, I know nothing about `zsh`, so if you want to use `zsh`, you'll have to write a detailed answer for me to accept it. – DeltaIV Dec 08 '21 at 13:26
  • As an aside, there seems to be no reason to `export` either of the variables here. There is a plethora of Bash command-line parsing questions; I nominated a popular one as a duplicate, but you can probably find more, and/or more specific ones. – tripleee Dec 08 '21 at 13:30
  • @DeltaIV : No, you have, of course, also to respect the differences in shell language. However, in case you like zsh (I for instance prefer zsh's way to use variables over bash, while other people dislake in particular this aspect), perhaps you will use zsh later for other tasks too. I had started with bash some time ago, and now found that I strongly prefer zsh for all aspects of shell programming. This is a purely subjective opinion, and I even found people who prefer ksh or fish for scripting. Also, zsh does have enough similarities to bash, to make the transition not too difficult. – user1934428 Dec 08 '21 at 13:55

0 Answers0