|
[Tutorial] Bash Scripting
|
|
09-08-2011, 01:03 PM
(This post was last modified: 09-09-2011 01:47 PM by Wolf95.)
|
|||
|
|||
|
[Tutorial] Bash Scripting
I've been Bash Scripting for quite some time now and being that it is VERY useful for VPSs, I figure it may be helpful for some of you, so here it goes:
BASH (Bourne Again SHell) is the default shell for distributions such as Debian, SuSE uses SH, which in my perspective isn't as feature rich, but if you have both installed you can toggle between shells inside of your script by using the "SH-bang" which is like this for BASH Code: #!/bin/bashCode: #!/bin/shDisplaying input: echo - Prints Text/Variables to screen, often use quotation marks so it looks like this: Code: #!/bin/bashCode: hello this is text that will display on screenVariables: Variables - Do not have to be registered to be used, you can register a variable like this: Code: #!/bin/bashCode: helloIF this... Then Do.... Else... If statements are pretty easy in bash, they go like this: Code: #!/bin/bashReading user input read variable - This will take the input from the user and store it in $variable Code: #!/bin/bashPerforming Commands You can have your shell script perform commands, here i will demonstrate a minecraft server installer. Code: #!/bin/bashAssigning Variables to Commands: You can assign variables to commands using backticks (`) Code: #!/bin/bashI'll add more to this later, maybe. Added 9/8/2011======================= This next part becomes more difficult, basically it pipes output from a program and selects a part of it to perform a action with. For Example: I will display the amount of free Ram on the System. Code: #!/bin/bashgrep and awk work together to select text, if you have played around in linux you know that the unprocessed output of free -t -m is: Code: root@debian:~# free -t -mCode: Mem: 2026 1208 818 0 5 685notice the second section using awk: $freeram=`free -t -m | egrep Mem | awk '{print $4}'` this will select the fourth block of text, in this case, 818. Alright, taking a little detour from that we will now take a look at loops, Loops - Allow you to perform actions such as parsing files or, cycling through variables, files, etc. I know you see no purpose in the example, but it pretty much gets the concept down: Code: #!/bin/bashThere is an advanced way of performing if commands, This is usually used in init scripts/Programs with menus, In this example I will also combine how to handle input variables as well. Note: Each Input variable is handled as $1.. $2... etc. so for this: script.sh hello nosql $1 would equal hello and $2 would equal no sql Case basically a long if-statement Code: #!/bin/bashThose end that code block, and the esac ends the case function. |
|||
|
09-08-2011, 01:32 PM
|
|||
|
|||
|
RE: [Tutorial] Bash Scripting
Nice tutorial on bash. I need to learn it.
.
|
|||
|
09-08-2011, 04:44 PM
|
|||
|
|||
|
RE: [Tutorial] Bash Scripting
Thank for nice tutorial Bash Scripting. I also need to learn about it too
Millions Thank FreeVPS & LoomHosts for my lovely VPS8 |
|||
|
09-08-2011, 05:18 PM
|
|||
|
|||
|
RE: [Tutorial] Bash Scripting
nice introduction of bash script.
its look like php right? |
|||
|
09-09-2011, 05:16 AM
|
|||
|
|||
|
RE: [Tutorial] Bash Scripting
Good intro to BASH scripting
![]() Please add more ! . . Thank you freeVps and Loomhosts for the amazing VPS . . |
|||
|
09-09-2011, 01:48 PM
|
|||
|
|||
|
RE: [Tutorial] Bash Scripting
Thanks for the positive feedback, I've added more, and it is very similar to PHP, that's why I think I caught on to PHP so easily.
|
|||
|
09-09-2011, 11:21 PM
|
|||
|
|||
|
RE: [Tutorial] Bash Scripting
Nice tutorial.
Don't worry about the world coming to an end today. It's already tomorrow in Australia. |
|||
|
09-16-2011, 03:45 AM
|
|||
|
|||
|
RE: [Tutorial] Bash Scripting
Hello,Thanks!!This is really a good tutorial!
I learn a bit from the bash language,and thank you,i wil find the bash language and learn! Thanks Carstensz Pyramid Server and FreeVPS very much my VPS18.
|
|||
|
« Next Oldest | Next Newest »
|
| User(s) browsing this thread: |
| 1 Guest(s) |






.

