LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
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 08-12-2004, 09:39 PM   #1
xviddivxoggmp3
Member
 
Registered: Feb 2004
Location: scanf
Distribution: Redhat Enterprise 4.4 AS
Posts: 236

Rep: Reputation: 30
bash scripts and running .sh files


I'm assuming from what I've read, bash scripts are saved as .sh files.
how would you run these files?
is the ./nameofscript.sh
 
Old 08-12-2004, 09:45 PM   #2
ppuru
Senior Member
 
Registered: Mar 2003
Location: Beautiful BC
Distribution: RedHat & clones, Slackware, SuSE, OpenBSD
Posts: 1,791

Rep: Reputation: 50
keepnig the .sh extension is good practice but not a necessity. Even if you name a bash script without the .sh extension, it will work. You would need to assign execute perms for that file. Another way to run these files is

source filename
 
Old 08-12-2004, 09:54 PM   #3
xviddivxoggmp3
Member
 
Registered: Feb 2004
Location: scanf
Distribution: Redhat Enterprise 4.4 AS
Posts: 236

Original Poster
Rep: Reputation: 30
i'm not sure what you mean by another way to run these scripts.

does this mean the word

source

is predefined to execute the script named in the current direcotry?

Code:
source myfile.sh
can you run scripts by using
Code:
./myfile.sh

Last edited by xviddivxoggmp3; 08-12-2004 at 09:55 PM.
 
Old 08-12-2004, 09:55 PM   #4
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
In linux the extension of a file is unimportant. The first line of the bash script will contain the line:
#!/bin/sh
or
#!/bin/bash.
What is important is that the executable bit is set. If you write your own script you need to use the 'chmod' command to allow it to be executed. For example: 'chmod u+x' allows the owner of the script to execute it but not anyone else. 'chmod a+x' enables execution by anybody. Of course you also need to set the 'r' read permission bit. i.e. 'chmod a+rx'

Also to be able to run a script, it needs to be located in one of the directories listed in your PATH environment variable. Or you need to include the pathname with the command.

Linux by default takes the precaution of not including the current directory in the path. To execute a file in the current directory precede the name with './' .

Like this:
./nameofscript.sh

You may have a bin directory in your home directory that is included in your path. This is a good place to copy scripts that you write yourself.
chmod u+x myscript
cp myscript ~/bin/

To learn the basic commands in linux, and how permissions work, a good place is the info page for coreutils. Try entering 'info:coreutils' or 'man:coreutils' in the konqueror browser (or in the application launcher). This is a convenient way to look at man and info pages.
 
Old 08-12-2004, 09:59 PM   #5
xviddivxoggmp3
Member
 
Registered: Feb 2004
Location: scanf
Distribution: Redhat Enterprise 4.4 AS
Posts: 236

Original Poster
Rep: Reputation: 30
can either one of you or anyone else in the linux community recommend a good bash script book or online tutorial?
I'm using Linux in a nutshell, and it isn't to detailed.
for instance i have been unable to find were it talks about using.
Code:
#!/bin/bash
 
Old 08-12-2004, 10:23 PM   #6
ppuru
Senior Member
 
Registered: Mar 2003
Location: Beautiful BC
Distribution: RedHat & clones, Slackware, SuSE, OpenBSD
Posts: 1,791

Rep: Reputation: 50
From the bash man page

If the program is a file beginning with #!, the remainder of the first
line specifies an interpreter for the program. The shell executes the
specified interpreter on operating systems that do not handle this exe-
cutable format themselves. The arguments to the interpreter consist of
a single optional argument following the interpreter name on the first
line of the program, followed by the name of the program, followed by
the command arguments, if any
 
Old 08-12-2004, 10:37 PM   #7
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
This web-page provides plenty of examples you can try out yourself.
http://www.tldp.org/LDP/abs/html/

The www.tldb.org (The Linux Documentation Project) is a must have book mark. They have all of the HOWTOs, man-pages and larger guides.
 
Old 08-13-2004, 07:35 PM   #8
xviddivxoggmp3
Member
 
Registered: Feb 2004
Location: scanf
Distribution: Redhat Enterprise 4.4 AS
Posts: 236

Original Poster
Rep: Reputation: 30
jschiwal,
Thanks for the site.
http://www.tldp.org/LDP/abs/html/
That is exactly what I needed.
I was trying to find something like that on that site,
but was unable to find it.
 
Old 08-13-2004, 09:04 PM   #9
xviddivxoggmp3
Member
 
Registered: Feb 2004
Location: scanf
Distribution: Redhat Enterprise 4.4 AS
Posts: 236

Original Poster
Rep: Reputation: 30
bash scripts with c++

how would i execute bash scripts with c++
 
Old 08-14-2004, 05:33 AM   #10
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,362

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
In C you could use the system lib call eg:
system("/usr/bin/myscript.sh");
I assume C++ has a similar facility.
NB: always a good idea to specify the full path to avoid being hijacked....
 
  


Reply



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
little help with bash scripts sniff Programming 2 10-26-2005 05:17 PM
Copying linked files, replacing directories in bash scripts? ta0kira Programming 2 10-10-2004 04:46 AM
$? in Bash scripts clinton Linux - Newbie 4 02-20-2004 11:15 AM
Running bash script files in C++ (Kylix) Acherion Programming 4 07-25-2003 11:35 PM
Running bash scripts from C++ application bfkeats Programming 1 06-18-2003 05:44 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:51 PM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration