Jump to content

My own Shell Script? (Ubuntu)

BrownZeus

Hey guys

 

I wanna make my own bash command or something that'll do a few things for me automatically.

 

I installed xampp on my computer for school, and there are a few steps to get the GUI for Xampp going (There is no dedicated "program" or shortcut). I have to use terminal to navigate to root/opt/lampp and then run the manager file to get the gui.

 

Can anyone link me to a good guide that'll actually help me understand what to do? I'd like to maybe make more scripts in the future as well.

Google hasn't turned up very fruitful in terms of understanding the process and doing what I want.

 

TIA

Link to comment
Share on other sites

Link to post
Share on other sites

4 minutes ago, BrownZeus said:

Hey guys

I wanna make my own bash command or something that'll do a few things for me automatically.

 

I installed xampp on my computer for school, and there are a few steps to get the GUI for Xampp going (There is no dedicated "program" or shortcut). I have to use terminal to navigate to root/opt/lampp and then run the manager file to get the gui.

 

Can anyone link me to a good guide that'll actually help me understand what to do? I'd like to maybe make more scripts in the future as well.

Google hasn't turned up very fruitful in terms of understanding the process and doing what I want.

TIA

So bash scripts are super basic - you can write your commands into them almost as if you were typing them into the bash shell or terminal. There's a great bash walkthrough linked below that goes over the basics of writing simple bash scripts. It's kind of like learning a programming language, but less about low-level logic and more about writing a script that handles a bunch of tasks all at once.

http://tldp.org/LDP/Bash-Beginners-Guide/html/index.html

http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_02_01.html

Desktop: KiRaShi-Intel-2022 (i5-12600K, RTX2060) Mobile: OnePlus 5T | Koodo - 75GB Data + Data Rollover for $45/month
Laptop: Dell XPS 15 9560 (the real 15" MacBook Pro that Apple didn't make) Tablet: iPad Mini 5 | Lenovo IdeaPad Duet 10.1
Camera: Canon M6 Mark II | Canon Rebel T1i (500D) | Canon SX280 | Panasonic TS20D Music: Spotify Premium (CIRCA '08)

Link to comment
Share on other sites

Link to post
Share on other sites

4 minutes ago, kirashi said:

So bash scripts are super basic - you can write your commands into them almost as if you were typing them into the bash shell or terminal. There's a great bash walkthrough linked below that goes over the basics of writing simple bash scripts. It's kind of like learning a programming language, but less about low-level logic and more about writing a script that handles a bunch of tasks all at once.

http://tldp.org/LDP/Bash-Beginners-Guide/html/index.html

http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_02_01.html

Awesome, thanks!

Link to comment
Share on other sites

Link to post
Share on other sites

4 hours ago, BrownZeus said:

I wanna make my own bash command or something that'll do a few things for me automatically.

Respect. 

4 hours ago, BrownZeus said:

I installed xampp on my computer for school,

Why? You can write your programs in Nano which is built in and does everything you need. 

4 hours ago, BrownZeus said:

and there are a few steps to get the GUI for Xampp going (There is no dedicated "program" or shortcut). I have to use terminal to navigate to root/opt/lampp and then run the manager file to get the gui.

 

Can anyone link me to a good guide that'll actually help me understand what to do? I'd like to maybe make more scripts in the future as well.

Google hasn't turned up very fruitful in terms of understanding the process and doing what I want.

I haven't anything but google though if you learn the Bash Terminal commands, you should know a lot of Sh scripts. 

 

As far as I know, Bash scripts are just files that do all the commands for you quickly. 
If you know some basic programming, & have a task you want to do, it's pretty easy to learn. Since you're in school, are you taking a Linux class? Linux classes teaches quite a lot of the Bash command line & then if you know some programming you can kinda figure out the rest where you use google to find answers to specific things like "How do I make a while loop in Shell Scripting Ubuntu"

 

Someone suggested a good start is to make a script that automatically backs up your hard drive or formats a drive. 

Link to comment
Share on other sites

Link to post
Share on other sites

16 hours ago, fpo said:

Respect. 

Why? You can write your programs in Nano which is built in and does everything you need. 

I haven't anything but google though if you learn the Bash Terminal commands, you should know a lot of Sh scripts. 

 

As far as I know, Bash scripts are just files that do all the commands for you quickly. 
If you know some basic programming, & have a task you want to do, it's pretty easy to learn. Since you're in school, are you taking a Linux class? Linux classes teaches quite a lot of the Bash command line & then if you know some programming you can kinda figure out the rest where you use google to find answers to specific things like "How do I make a while loop in Shell Scripting Ubuntu"

 

Someone suggested a good start is to make a script that automatically backs up your hard drive or formats a drive. 

Thanks

Responding directly to your last two points, its just a class that's briefly touching on a few Web Development practices and such. Xampp as you may or may not already know is just a W/L/MAMPP stack package that acts as an easier way to manage the stack without having to individually work with each member of the stack. As far as Linux goes, I know a little bit of my way around it, given it has been my primary OS for a year now. I switched to it because I am a CS major and figure since Linux is pretty versatile and popular in the trade I should get used to it/be familiar with it. Unfortuneatly my school doesn't offer a dedicated class to learning Linux. So as a result I've just been trying to teach myself as i go.

 

 

Link to comment
Share on other sites

Link to post
Share on other sites

If you know a significant amount of the Bash (I think Ubuntu’s default terminal) you should have little problem making bash scripts. 

Link to comment
Share on other sites

Link to post
Share on other sites

Where I may be putting too much though into this, I would start with what fpo has said, Bash/Shell can be as simple as writing commands on diffrent lines or with a ';' to split commands. this is a good start point with out known what you want to do it can be hard to say where to suggest to start.  But when ever I have to plan a script I ask myself the following (also sorry if i am going too basic):

 

1. what do i want it to do
2. where am i stating from
3. what are my use cases
4. what can go wrong
5. when something goes wrong how should that be handled

 

so lets start with your example which is to launch lampp.  to do this in the command line you would need to run as root. and enter the following command:
 
/root/opt/lampp

 

this covers part 1 (what do i want the script to do)

 

step 2 where am i starting from: in this case this I will assume you are running from userspace i.e. /home/username with an account which is in the wheel or sudo group (this means that you can run the sudo command)

 

step 3, what can go wrong.
in this case if the user is not 'root' or using the 'sudo' command then the it will fail to run so we should look at how to warn the user that this is the case.  to do this I typically run a 'whoami' command:

 

so i would put the command(s) which i want to run surrounded by a if statment:

 

#!/bin/bash

#test to see if the user is either root / sudo 
if [ $(whoami | grep -c root) -eq 1 ]; then
    echo "user is root"
else
    echo "[!] Fail the user is not running as root or sudo"
fi

 

since you are new to bash / scripting line 1 of the code i.e. '#!/bin/bash' is called a shebang and the function of this
line is to help the system know how to run this program, in this case we want to run the program using the bash Shell
so it is /bin/bash, is it was a python script then it would be '#!/usr/bin/python3'

line 3 '#test to see if the user is either root/sudo' this is a comment.

line 4 has the if statement and the then (which runs the if statement)

the simpe version of a if statement is 'if [ condition ]' it is worth noteing the space each side of the 'if' statement
as bash will not work with out it.

inside the condition section of the if statment, i am running '$()' which is saying the following:
    1. evaluate what is in the brackets '(whoami | grep -c root)'
    2. store it as variable '$'
    3. is it equal '-eq' to 1

(note the grep command simply means greedy regular expression and it searches keyword(s) or phrases and the -c switch
simply counts the number of occurances)

the ';' means end of statement this can also be done by using a new line.
the 'then' keyword means if this then do this ....

line 5 - this is just to show if the condition is true
line 6 - 'else' is incase the codition does not evaluate to true, this is optional. however in this example
we want to use it to warn the user they need to run as root / sudo.

line 7 - this is where tell the user that the script will not run
line 8 - 'fi'  this is how end a 'if' statement block;

 

 

once you get used to if / elif / else statments and looking what can go wrong, you should be used to the
syntax enougth to run while loops: https://bash.cyberciti.biz/guide/While_loop

and after that I will be asking you for help ^^

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×