Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place! |
Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
|
01-03-2008, 07:09 AM
|
#1
|
Member
Registered: Oct 2007
Posts: 53
Rep:
|
shell scripting/ creating new commands
hellow,
i would like to create new commands and use them without having to access the file each time.
do i use functions or aliases to create them?
how can i make them accessible?
thx
maya
|
|
|
01-03-2008, 07:16 AM
|
#2
|
Moderator
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417
|
what does "accessible" mean here? what file would you have to access?
maybe you want to know about the source command, which is similar to a C++ #include.
|
|
|
01-03-2008, 07:31 AM
|
#3
|
Member
Registered: Oct 2007
Posts: 53
Original Poster
Rep:
|
accessible means "being able to use them!"
i want to use the commands without having to go to where the file is, open it and run the commands , i want to use them like built-in commands !
|
|
|
01-03-2008, 07:34 AM
|
#4
|
Member
Registered: Oct 2007
Posts: 53
Original Poster
Rep:
|
how can i use a function i created?
Last edited by mayaabboud; 01-03-2008 at 07:38 AM.
|
|
|
01-03-2008, 07:37 AM
|
#5
|
Moderator
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417
|
Quote:
Originally Posted by mayaabboud
accessible means "being able to use them!"
i want to use the commands without having to go to where the file is, open it and run the commands , i want to use them like built-in commands !
|
ok, so what is stopping you doing that then...? create a bash file and put it somewhere on your path.. what's the problem you're actually facing?
|
|
|
01-03-2008, 07:40 AM
|
#6
|
Member
Registered: Oct 2007
Posts: 53
Original Poster
Rep:
|
i created a .profile file,
and i wrote a function in it called 'histoire'
i added the .profile file to my path
but only when calling the file, it works, but by calling the function it doesnt !!
|
|
|
01-03-2008, 07:43 AM
|
#7
|
Moderator
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417
|
well as above i think you want the source command, but it's really hard to work out what you actually mean...
|
|
|
01-03-2008, 07:46 AM
|
#8
|
Member
Registered: Oct 2007
Posts: 53
Original Poster
Rep:
|
what i actually mean is very simple,
i have the .profile file that contains the following program:
# !/bin/bash
#profile program
function histoire {
HISTSIZE=20
echo “HISTSIZE est égale à” $HISTSIZE
echo “le numéro de la commande est” $fc $-l
}
histoire
exit 0
i have here the function called 'histoire'
i would like to write on my xterm just the word histoire and get the results of the program,
it DOES run when i write .profile,
but i want to write several functions in the same program, and
then choose which one to use ,
not all at once !
|
|
|
01-03-2008, 07:47 AM
|
#9
|
LQ Veteran
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809
|
If you write a script, you can make it accessible by putting it in a directory which is in $PATH---or by modifying $PATH to include the directory.
"echo $PATH" to see what the current setup is.
|
|
|
01-03-2008, 07:50 AM
|
#10
|
LQ Veteran
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,836
|
1. create eg. 'bin' directory in your home directory
2. when you create a script, put it to ~/bin/
3. chmod +x ~/bin/name_of_the_script
4. modify your ~/.bashrc
Add
5.
Last edited by sycamorex; 01-03-2008 at 07:51 AM.
|
|
|
01-03-2008, 07:51 AM
|
#11
|
Member
Registered: Oct 2007
Posts: 53
Original Poster
Rep:
|
i already did
export PATH=$PATH:<nameofdirectory>
but that only helps by letting me open the file wherever i am , but doesnt help with the function !!
|
|
|
01-03-2008, 07:51 AM
|
#12
|
LQ Veteran
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809
|
If you have multiple functions inside one script file, the only way to call them individually is to call the script with arguments.
Why not just make one file for each function?
|
|
|
01-03-2008, 07:57 AM
|
#13
|
Member
Registered: Oct 2007
Posts: 53
Original Poster
Rep:
|
oh yes
$ .profile histoire worked !!
that was actually what i was looking for
well 2 write a file for each function would be too easy
i wanted to find a way to do it in a smart way
by the way, i am working on a Knoppix cd because i dont have Linux installed directly on my computer, so i can only work in my tmp directory !
thanks alot
maya
|
|
|
01-03-2008, 08:03 AM
|
#14
|
ReliaFree Maintainer
Registered: Aug 2004
Location: Kalamazoo, Michigan
Distribution: Slackware 14.2
Posts: 2,815
|
You don't need the file in your PATH, but Bash only reads ~/.profile if invoked as an interactive login shell, or with the --login option. But....it looks for ~/.bash_profile, ~/.bash_login, and ~/.profile, in that order, and reads and executes commands from the first one that exists and is readable. If you execute Bash as an interactive shell, but NOT a login shell, Bash reads and executes commands from ~/.bashrc if the file exists.
So, typically, a ~/.bash_profile contains the line
Code:
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
after (or before) any login-specific initializations. Put your function histoire in ~/.bashrc, make sure you have the above code in your ~/.bash_profile and see if that does what you want. You will be able to type histoire at the command prompt and execute the function. You can have as many functions as you want in ~/.bashrc. At least I think that's what you want.
Last edited by weibullguy; 01-03-2008 at 08:07 AM.
|
|
|
01-03-2008, 08:06 AM
|
#15
|
LQ Veteran
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,836
|
Quote:
well 2 write a file for each function would be too easy
i wanted to find a way to do it in a smart way
|
Well, IMHO putting each function in a separate script would be the 'smart' way, LOL
But that's just my humble opinion 
|
|
|
All times are GMT -5. The time now is 05:50 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|