Bash Scripting Tutorial - Printable Version +- Support Forums (https://www.supportforums.net) +-- Forum: Categories (https://www.supportforums.net/forumdisplay.php?fid=87) +--- Forum: Computer Support Topics (https://www.supportforums.net/forumdisplay.php?fid=4) +---- Forum: Linux, FreeBSD, and Unix (https://www.supportforums.net/forumdisplay.php?fid=17) +---- Thread: Bash Scripting Tutorial (/showthread.php?tid=2061) |
Bash Scripting Tutorial - bsdpunk - 10-23-2009 So everybody is doing there little .bat files for windows, right. Well what is the equivalent in linux? Typically the shell installed with linux is bash(bourne again shell, it's a joke l'll explain it to you one day). Ok so here is how you write a bash shell, open your favorite text editor, mine is vi and type the following. Code: #!/bin/bash To run the script without chaning your unix permisions you can type: bash test.sh In the directory it is in. Or if you want to change your unix permissions and run the script traditionally you can type: chmod 755 test.sh Then you can run it the normal way which is to type: ./test.sh Make sure your in the same directory. If you are not in the same directory you can run it by typing the full path, like this(but with your path) /home/lane/scripts/test.sh Ok so a couple caveats with this thing. 1) your nix enviroment has to have bash, but most do and 2) your bash must be in /bin/bash which for most nix machines, that is where it is. But like on my machine which is a little diffrent I have to have this as my first line #!/usr/local/bin/bash because my bash is in /usr/local/bin/bash. 3) Make sure finger is on your system and you type a username that is logged in And this won't run the same in all environments, and if you copy and paste it make sure you pay attention to the punctuation and make sure it didn't change, before I tested this I wrote it on a windows machine in word and when I pasted it in vi in my putty ssh session some of the punctuation changed. Another thing is, if you just copy and paste it you may have problems because of something called magic quotes. The quotation marks may change to the kind that lean inwards, and bash see's that as just a letter not as a quotation marks, so if your not typing it in(which you should think about doing since it helps the learning process) and it just prints the script out, instead of running it replace the quotation marks manually. Bash is a lot more powerful than .bat, and when I do get stuck on windows systmes, I install a program called cygwin, so that I have a bash enviroment on my windows machine and can automate things like backing up a drive, with tools like rsync. Alright have fun. RE: Bash Scripting Tutorial - Gaijin - 10-23-2009 Thanks for this bsdpunk, I need to learn about bash and this helped me with the first step! |