LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   shell scripting/ creating new commands (https://www.linuxquestions.org/questions/linux-newbie-8/shell-scripting-creating-new-commands-610806/)

mayaabboud 01-03-2008 07:09 AM

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

acid_kewpie 01-03-2008 07:16 AM

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.

mayaabboud 01-03-2008 07:31 AM

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 !

mayaabboud 01-03-2008 07:34 AM

how can i use a function i created?

acid_kewpie 01-03-2008 07:37 AM

Quote:

Originally Posted by mayaabboud (Post 3009660)
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?

mayaabboud 01-03-2008 07:40 AM

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 !!

acid_kewpie 01-03-2008 07:43 AM

well as above i think you want the source command, but it's really hard to work out what you actually mean...

mayaabboud 01-03-2008 07:46 AM

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 !

pixellany 01-03-2008 07:47 AM

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.

sycamorex 01-03-2008 07:50 AM

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

mayaabboud 01-03-2008 07:51 AM

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 !!

pixellany 01-03-2008 07:51 AM

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?

mayaabboud 01-03-2008 07:57 AM

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

weibullguy 01-03-2008 08:03 AM

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.

sycamorex 01-03-2008 08:06 AM

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 11:58 PM.