LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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


Reply
  Search this Thread
Old 01-03-2008, 08:09 AM   #1
mayaabboud
Member
 
Registered: Oct 2007
Posts: 53

Rep: Reputation: 15
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
 
Old 01-03-2008, 08:16 AM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974
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.
 
Old 01-03-2008, 08:31 AM   #3
mayaabboud
Member
 
Registered: Oct 2007
Posts: 53

Original Poster
Rep: Reputation: 15
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 !
 
Old 01-03-2008, 08:34 AM   #4
mayaabboud
Member
 
Registered: Oct 2007
Posts: 53

Original Poster
Rep: Reputation: 15
how can i use a function i created?

Last edited by mayaabboud; 01-03-2008 at 08:38 AM.
 
Old 01-03-2008, 08:37 AM   #5
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974
Quote:
Originally Posted by mayaabboud View Post
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?
 
Old 01-03-2008, 08:40 AM   #6
mayaabboud
Member
 
Registered: Oct 2007
Posts: 53

Original Poster
Rep: Reputation: 15
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 !!
 
Old 01-03-2008, 08:43 AM   #7
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974
well as above i think you want the source command, but it's really hard to work out what you actually mean...
 
Old 01-03-2008, 08:46 AM   #8
mayaabboud
Member
 
Registered: Oct 2007
Posts: 53

Original Poster
Rep: Reputation: 15
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 !
 
Old 01-03-2008, 08:47 AM   #9
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
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.
 
Old 01-03-2008, 08:50 AM   #10
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,819
Blog Entries: 1

Rep: Reputation: 1200Reputation: 1200Reputation: 1200Reputation: 1200Reputation: 1200Reputation: 1200Reputation: 1200Reputation: 1200Reputation: 1200
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
Quote:
PATH=$PATH:~/bin
5.
Quote:
. ~/.bashrc

Last edited by sycamorex; 01-03-2008 at 08:51 AM.
 
Old 01-03-2008, 08:51 AM   #11
mayaabboud
Member
 
Registered: Oct 2007
Posts: 53

Original Poster
Rep: Reputation: 15
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 !!
 
Old 01-03-2008, 08:51 AM   #12
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
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?
 
Old 01-03-2008, 08:57 AM   #13
mayaabboud
Member
 
Registered: Oct 2007
Posts: 53

Original Poster
Rep: Reputation: 15
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
 
Old 01-03-2008, 09:03 AM   #14
weibullguy
ReliaFree Maintainer
 
Registered: Aug 2004
Location: Kalamazoo, Michigan
Distribution: Slackware-current, Cross Linux from Scratch, Gentoo
Posts: 2,812
Blog Entries: 1

Rep: Reputation: 259Reputation: 259Reputation: 259
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 09:07 AM.
 
Old 01-03-2008, 09:06 AM   #15
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,819
Blog Entries: 1

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


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
Shell Scripting: Getting a pid and killing it via a shell script topcat Programming 15 10-28-2007 03:14 AM
teaching shell scripting: cool scripting examples? fax8 Linux - General 1 04-20-2006 05:29 AM
shell scripting commands and yum= geminigal Linux - Newbie 2 05-13-2005 04:12 AM
shell scripting -- passing commands mehesque Linux - General 4 06-25-2004 11:28 AM
I need a scripting lanuage with case and shell commands rob.rice Programming 1 04-18-2004 03:17 PM


All times are GMT -5. The time now is 05:00 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration