LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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 12-05-2007, 02:52 PM   #1
whited
Member
 
Registered: Feb 2005
Distribution: Slackware 11
Posts: 109

Rep: Reputation: 15
Bash Script as command


So I have a program.bash which is in my working directory. I want to be able to just right "program" without the ./ and have it run. Is there any way to do this?
 
Old 12-05-2007, 03:08 PM   #2
reddazz
LQ Guru
 
Registered: Nov 2003
Location: N. E. England
Distribution: Fedora, CentOS, Debian
Posts: 16,298

Rep: Reputation: 77
Make sure your script has something like
Code:
#!/bin/bash
or
Code:
#!/usr/bin/env bash
at the top. Make your script executable and then copy it to somewhere in your path e.g. /usr/local/bin.

Last edited by reddazz; 12-06-2007 at 01:22 AM. Reason: typos
 
Old 12-05-2007, 03:11 PM   #3
tronayne
Senior Member
 
Registered: Oct 2003
Location: Northeastern Michigan, where Carhartt is a Designer Label
Distribution: Slackware 32- & 64-bit Stable
Posts: 3,541

Rep: Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065
Rename the program to "program.sh" then type "make program" (shell program source is .sh, the make utility knows how to create an executable shell program from properly-named source).

In your home directory .profile or .bash_profile or whatever file you use to set environment variables, put
Code:
PATH=.:${PATH}
export PATH
Log out and log back in so that takes effect.
 
Old 12-05-2007, 04:37 PM   #4
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
shell code is run as source (actually interpreted). You don't use make on it. Just do as reddazz said.
 
Old 12-05-2007, 04:39 PM   #5
ilikejam
Senior Member
 
Registered: Aug 2003
Location: Glasgow
Distribution: Fedora / Solaris
Posts: 3,109

Rep: Reputation: 97
Just something to note - having '.' in your PATH is potentially not a great idea:

http://it-support-servicestatus.web....ATH-060607.htm

Dave
 
Old 12-05-2007, 05:07 PM   #6
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
Quote:
Originally Posted by ilikejam View Post
Just something to note - having '.' in your PATH is potentially not a great idea
Agreed, esp. not if you're using the root account. Making an alias in your shells resouce file would be "easier".
 
Old 12-06-2007, 08:43 AM   #7
tronayne
Senior Member
 
Registered: Oct 2003
Location: Northeastern Michigan, where Carhartt is a Designer Label
Distribution: Slackware 32- & 64-bit Stable
Posts: 3,541

Rep: Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065
Oh, golly.

OK, I learned this stuff the Bell Labs way -- you know, most of those shell programs in /bin (13) and /usr/bin (420) (plus others spread all over the place) that start life as prog.sh in a directory of mixed source code (mostly, in the case of Unix-like systems, C), living in a source code control system (used to be SCCS, nowadays more likely CVS) and built to executables and installed with the make utility. Thousands of source code files, well commented, with source code control keywords, the whole bit.

Silly me, I continue to follow that model; shell program source is prog.sh, type make run prog by typing its name and all is well with the world; kind of a if it's worth doing it's probably worth doing consistently, I suppose. I also embed a standard set of CVS keywords to keep track of what I did, why I did it, when I did it and all that kind of thing (simply because I can't remember a lot of stuff a week from now). I even have a standard template for all shell programs (yes, I use Korn shell, not BASH, but there you are -- my stuff has to run on Solaris boxes as well as Linux boxes):
Code:
#!/bin/ksh
#ident  "$Id$"
#
#       Name:           $Source$
#       Version:        $Revision$
#       Modified:       $Date$
#       Purpose:
#       Author:
#       Date:
#       $Log$
#
#       define a fatal error function
function fatal
{
        print -u2 "${1}"
        exit 1
}
#       initialize flags to null
aflag=
bflag=
#       define usage message
USAGE="Usage:\t${0} [-a value] [-b value] args"
#       process command line arguments
while getopts :?a:b: name
do
        case ${name} in
        a)      aflag=1
                aval="${OPTARG}";;
        b)      bflag=1
                bval="${OPTARG}";;
        :)      print -u2 "${0}:\t${OPTARG} requires a value"
                fatal "${USAGE}";;
        \?)     print -u2 "${0}:\tunknown option"
                fatal "${USAGE}";;
        esac
done
if [ ! -z "${aflag}" ]
then
        print "Option -a ${aval} specified"
fi
if [ ! -z "${bflag}" ]
then
        print "Option -b ${bval} specified"
fi
shift $((${OPTIND}-1))
if [ "$*" ]
then
        print "Remaining arguments are:\t${*}"
fi
exit 0
Is that overkill? Well, maybe, but I don't have to type a bunch of stuff every time I start a new shell program...

And current directory first on the path? Well, I've been doing this on and off since 1962, starting with punch cards (How was Thomas Watson buried? Nine edge down, that's how) and getting into early efforts at usable systems with GE then Honeywell GECOS then Multics, then Unix System 3, then System V and derivatives (you know, BSD, Sun OS, Linux, etc., etc.). Following that simplest of rules that thou shalt not work as root has served me well on my own Linux systems along with the hundred or so users I develop for on a Solaris farm. It's convenient, particularly if you're modifying a program that is installed in a production environment so you execute the one you're working on rather than the older production version.

I'm far more worried about installing open source software that a bad guy may have embedded a nasty in that gets installed with mode 4755 as root than I am about current directory first on path. It's always at the back of my mind that everything owned by root can lead to a catastrophe when somebody, somewhere pulls this one (I do a bug hunt on file modes every time I install something just to make sure). I used to install every add-on owned and grouped bin just to avoid this kind of thing but current practice seems to be that root owns it all and that's that.

Anyway, that's my two cents worth for what it's worth -- write it as prog.sh, make it, test it, edit the source, and repeat.
 
Old 12-06-2007, 09:56 AM   #8
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
For a script couldn't you use your revision control system directly to check out the script?
 
Old 12-06-2007, 01:57 PM   #9
tronayne
Senior Member
 
Registered: Oct 2003
Location: Northeastern Michigan, where Carhartt is a Designer Label
Distribution: Slackware 32- & 64-bit Stable
Posts: 3,541

Rep: Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065
Quote:
Originally Posted by jschiwal View Post
For a script couldn't you use your revision control system directly to check out the script?
Of course -- but I'd rather follow the source code separate from executable rule and use the system tools to get stuff "made" and installed where and how I want it installed. I don't like having executables in CVS (with the single exception of the configure program) and I find it much, much easier to realize what some source code file is based on the file name extension (but not ever having that extension hanging on an installed executable -- makes about as much sense to me to install prog.sh as an executable is it does to install prog.c, or, shudder, prog.exe as an executable on a Unix-based system).

And, really, this is a tempest in a teapot: Unix-like systems are meant to reflect the personal preferences of the owner (or maybe the group preferences of the organization) so no big deal one way or the other.
 
Old 12-06-2007, 04:47 PM   #10
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
I was only trying to point out that you don't need to make (as in compile/link) a shell prog, it's already to go. Of course you can use make to manage/install anything, but these days most people assume make=compile.
 
Old 12-06-2007, 08:09 PM   #11
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
What kind of dependencies could you have for a bash script. My point is that running make on a single script won't do anything.
 
Old 12-07-2007, 12:25 AM   #12
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Ah, but if you are strict with 'the source code separate from executable rule' as tronayne is, then you use make to 'generate' the executable, even though in this case that would mean a CVS checkout and/or a cp, but no compile/link.
If you use the src code ctrl throughout a proj (formally iow), you can 'make' the whole proj, inc documentation that way.
I've seen it done on very large projects.
It's nice because you get a defined clean product (inc docs) esp for external releases/clients.
Actually, in re keeping a copy of the exes in CVS (or whatever) it's not necessarily a bad idea. It means you can tell at a glance if the exe in prod is the same size as the one in dev. Of course if the toolset is upgraded then the same src may compile to diff size exes.
I does also give you an idea of the time lag between dev compiled ver and prod ie release delays (and possibly an indicator if the dates don't look right even if the size does....)
 
Old 12-07-2007, 06:48 PM   #13
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
I just realized that the source could be in a non-executable location, so you could have a make install target that would copy the script to ~/bin/.

I don't write production grade scripts. Just short ones I put in ~/bin/. There is a plugin for vim available that starts you off with a commented header similar to the own given on a post above. For most users, I think that should be enough formalization for their bash scripts.

The OP wanted to make the mistake of adding the current directory to his path, so this is probably beyond what he would want to do.

Last edited by jschiwal; 12-08-2007 at 05:09 AM.
 
Old 12-08-2007, 07:52 AM   #14
PAix
Member
 
Registered: Jul 2007
Location: United Kingdom, W Mids
Distribution: SUSE 11.0 as of Nov 2008
Posts: 195

Rep: Reputation: 40
In conclusion, Tronayne is perfectly correct, but just a little industrially over strengthed and formal for the average person asking this particular question.

Not particularly useful to the original questioner, but I think the point has been established as a viable option in an industrial strength formal production environment.

Now I know why we don't ask about driving quetions, in case the OP learned correctly how to drive on the other side of the road .
 
Old 12-08-2007, 08:33 AM   #15
tronayne
Senior Member
 
Registered: Oct 2003
Location: Northeastern Michigan, where Carhartt is a Designer Label
Distribution: Slackware 32- & 64-bit Stable
Posts: 3,541

Rep: Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065
Lesson learned during a wasted youth:
  • A quick and dirty shell program will turn into a permanent utility
  • The system won't work without that utility
  • Somebody, somewhere, sometime will blow away that utility
  • You will get a call at three in the morning
  • Better to be safe than sorry
It really doesn't hurt to have a src directory and a bin directory too.

Y'all have a great holiday!
 
  


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
Bash script says command not found - why? RavenLX Ubuntu 3 08-03-2007 02:19 PM
Bash Script- command on exit PDock Programming 6 12-23-2005 07:03 PM
Bash Script; command not found twintornado Programming 2 06-01-2005 09:59 AM
Bash script - repeated command satimis Programming 27 10-28-2004 05:02 AM
Simple Bash Script Help Command kemplej Linux - Software 1 03-11-2004 03:52 AM

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

All times are GMT -5. The time now is 11:41 AM.

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