P
P
PlayBoyXXX2021-09-11 22:36:28
linux
PlayBoyXXX, 2021-09-11 22:36:28

How to run a function in bash from the command line?

The script has a function:

Autobak(){
  zip -r openvpn.zip /etc/openvpn
  backupURL="$(curl -F "[email protected]/root/openvpn.zip" https://file.io | jq '.link')"
  curl -s -X POST https://api.telegram.org/bot"$bot_api"/sendMessage -d chat_id="$tgid" -d text="Бэкап сервера OpenVPN $backup_serv_id  Дата: $(date) Ссылка на бэкап: $backupURL"
  rm /root/openvpn.zip
}

How can I make it so that I can run it directly from the command line?
Thank you very much in advance.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
K
ky0, 2021-09-11
@PlayBoyXXX

Bind this function to some argument and use case.

Autobak()
...
case "$1" in
    "foo")
    Autobak
;;
esac

and then ./script.sh foo.

P
pfg21, 2021-09-11
@pfg21

1. Decorate this function with a separate bash script, with the name of this function.
2. In the body of the main function of the script, make the option to launch this particular function.

S
Saboteur, 2021-09-11
@saboteur_kiev

1. Just execute the function on the command line.
2. Add it to .bashrc or .profile so that it is loaded on login
3. Execute your script with the function in the current shell, it will be available after that (

$ source script_with_function.sh
$ Autobak

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question