Tuesday, May 14, 2024
 Popular · Latest · Hot · Upcoming
5
rated 0 times [  5] [ 0]  / answers: 1 / hits: 8992  / 2 Years ago, wed, november 9, 2022, 3:21:45

I'm extremely new to bash and need to create a script that calls another script depending on what command line switch is taken.



Let's call it stuff.sh



than stuff.sh --help should behave like help does in a normal program and give a list to the user what he can do




  • -a you can do stuff like that.

  • -b you can do stuff like that.

  • -c this is extremely fancy stuff.



When I execute the script with stuff.sh - a it should do something, let's say call another script with sudo in front of it.



How could I do that?



any Ideas that everybody how is new to bash can understand easily?



Thank you!


More From » scripts

 Answers
3
#!/bin/bash

function show_help() {
cat << ENDHELP
-a you can do stuff like that.
-b you can do stuff like that.
-c this is extremely fancy stuff.
ENDHELP
}

case $1 in
--help)
show_help
;;
-a)
sudo /path/to/other/script
;;
-b)
do_some_stuff
do_another_stuff
;;
-c)
do_extremely_fancy_stuff
do_another_extremely_fancy_stuff
run_as_many_commands_as_you_want
;;
*)
echo "Run $0 --help"
;;
esac



  • $0 is the script name.

  • $1 is the first command line argument, $2 is the second, and so on.



Read bash manpage for reference.


[#30626] Thursday, November 10, 2022, 2 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
laceanz

Total Points: 86
Total Questions: 113
Total Answers: 104

Location: Australia
Member since Thu, Jan 26, 2023
1 Year ago
laceanz questions
Tue, Apr 26, 22, 07:43, 2 Years ago
Sat, Oct 23, 21, 09:40, 3 Years ago
Sat, Nov 5, 22, 08:24, 2 Years ago
Sat, Mar 12, 22, 07:56, 2 Years ago
;