LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 07-02-2008, 10:32 AM   #1
BallsOfSteel
Member
 
Registered: Mar 2008
Location: Florida
Distribution: Fedora mainly, but I am open to others.
Posts: 273

Rep: Reputation: 33
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
 
Old 07-02-2008, 10:56 AM   #2
Hobbletoe
Member
 
Registered: Sep 2004
Location: Dayton, Oh
Distribution: Linux Mint 10, Linux Mint 11
Posts: 148

Rep: Reputation: 18
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.
 
Old 07-02-2008, 11:04 AM   #3
unSpawn
Moderator
 
Registered: May 2001
Posts: 27,451
Blog Entries: 54

Rep: Reputation: 2894Reputation: 2894Reputation: 2894Reputation: 2894Reputation: 2894Reputation: 2894Reputation: 2894Reputation: 2894Reputation: 2894Reputation: 2894Reputation: 2894
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.
 
Old 07-02-2008, 11:04 AM   #4
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,288

Rep: Reputation: 174Reputation: 174
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

Last edited by bigearsbilly; 07-02-2008 at 11:05 AM.
 
Old 07-02-2008, 11:10 AM   #5
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1948Reputation: 1948Reputation: 1948Reputation: 1948Reputation: 1948Reputation: 1948Reputation: 1948Reputation: 1948Reputation: 1948Reputation: 1948Reputation: 1948
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.
 
Old 07-02-2008, 11:17 AM   #6
BallsOfSteel
Member
 
Registered: Mar 2008
Location: Florida
Distribution: Fedora mainly, but I am open to others.
Posts: 273

Original Poster
Rep: Reputation: 33
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
 
Old 07-02-2008, 12:51 PM   #7
telecom_is_me
Member
 
Registered: Jun 2008
Location: Upstate NY
Distribution: Fedora on the desk / Gentoo in the Racks
Posts: 36

Rep: Reputation: 15
Quote:
Originally Posted by BallsOfSteel View Post
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.
 
Old 07-02-2008, 08:07 PM   #8
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,287

Rep: Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034
Personally I prefer

chmod u+x arbitrary_name.sh

no need to give group/others execute perm (unless there really is)
 
Old 07-03-2008, 06:42 AM   #9
sandgroper
Member
 
Registered: Jul 2004
Location: Perth , Western Australia
Distribution: Fedora Core 5 , Mint 9
Posts: 118

Rep: Reputation: 15
Quote:
Originally Posted by chrism01 View Post
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
 
Old 07-04-2008, 12:48 AM   #10
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,287

Rep: Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034
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...
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash Script Help - Trying to create a variable inside script when run. webaccounts Linux - Newbie 1 06-09-2008 02:40 PM
Trying to create a transcoding script (BASH) Phyrexicaid Programming 3 10-15-2007 12:20 PM
Bash script to create bash script jag7720 Programming 10 09-10-2007 07:01 PM
How to create a bash script to count? Alinuxnoob Programming 4 01-28-2007 10:48 PM
create a bash script that restarts? nny0000 Linux - Software 7 09-15-2006 05:14 PM


All times are GMT -5. The time now is 10:50 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration