LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Create and run a bash script (https://www.linuxquestions.org/questions/programming-9/create-and-run-a-bash-script-653097/)

BallsOfSteel 07-02-2008 10:32 AM

Create and run a bash script
 
I want to create a shell script to run a few commands as opposed to having to enter them every time I want to do something.


Essentially it's about 6 commands that unloads a module and loads another for my wireless card and puts in a few settings. So, I know the commands, but I don't know how to create the shell script and run it. I know you do it in a text editor, just not sure what to save it as and how to run it when I want to.

If you could point me to a how-to or give me some pointers, I'd appreciate it.

Thanks

Hobbletoe 07-02-2008 10:56 AM

Advanced Bash Scripting Guide This has just about everything that you will ever need to know about BASH scripting, as well as a lot of examples.

unSpawn 07-02-2008 11:04 AM

That's a bit short plus the ABS might seem a bit intimidating to firsttimers. Basically a script contains the commands you run in your shell but with a starting line that reads "#!/path/shellname" and a ending line that reads "exit 0" (if you conform to standards) and some "basic logic" to deal with situations. If it's an initscript (you might call Wireless a networking subservice), things differ per distribution (please fill in your profile details) but at the same time you'll have the examples right there. Your basic Bash scripting guides are:
http://www.tldp.org/HOWTO/Bash-Prog-Intro-HOWTO.html
http://www.tldp.org/LDP/Bash-Beginne...tml/index.html
http://www.tldp.org/LDP/abs/html/ # this one not being that basic, OK.

Else just post your distro name and the commands.

bigearsbilly 07-02-2008 11:04 AM

just put in a file,

edit arbitrary_name.sh


(the #! line must be the first line)
Code:

#!/bin/bash
# or shell of choice /bin/sh, /bin/ksh

ls
date
echo hello

and chmod +x arbitrary_name.sh
then run it..
./arbitrary_name.sh

David the H. 07-02-2008 11:10 AM

I always recommend linuxcommand.org as a beginning-level shell/scripting tutorial. It's very easy to follow and provides a good grounding in the basics.

BallsOfSteel 07-02-2008 11:17 AM

Thanks for the help, I figured it out and couldn't post back fast enough. lol.

As always, you guys are a bunch of help.

Brandon

telecom_is_me 07-02-2008 12:51 PM

Quote:

Originally Posted by BallsOfSteel (Post 3201699)
Thanks for the help, I figured it out and couldn't post back fast enough. lol.

As always, you guys are a bunch of help.

Brandon

If you post the specific commands that you are trying to automate I would be happy to make a script for you.

chrism01 07-02-2008 08:07 PM

Personally I prefer

chmod u+x arbitrary_name.sh

no need to give group/others execute perm (unless there really is)

sandgroper 07-03-2008 06:42 AM

Quote:

Originally Posted by chrism01 (Post 3202270)
Personally I prefer

chmod u+x arbitrary_name.sh

no need to give group/others execute perm (unless there really is)

This is a small script that I use to make scripts executable , you can also create multiple executable scripts.

Code:

#!/bin/sh
# mkexe script to make files executable
# can take multiple file names as arguments
# 755 = rwxr-xr-x
if (test $# != 0)
then
        chmod 755 $*
        echo The file $* has been made executable
        pwd
        ls -al $*
else
        echo 'mkexe : arguments [ file1 ...... file N ]'

fi


chrism01 07-04-2008 12:48 AM

Actually, my point was you should only grant perms to those who need them, so normally only the owner needs eg 'rwx', not 'group' or 'others'.
ie
rwx------
If you need to share the functionality with your group, but not editing, then
rwxr-x---

too many people just go
chmod +x
which allows user,group and others aka world(!) execute perms. Not a good habit to get into...


All times are GMT -5. The time now is 06:40 AM.