user defined commands


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers user defined commands
# 1  
Old 02-11-2012
user defined commands

Hi, i would like to create user defined commands.
e,g:
if an user executes ,
Code:
mkdircd test

then a directory called test should be created and it should be cd to test.
How i can create the command mkdircd with below action:
Code:
mkdir $1 && cd $1

.
Please help me in achieving this

Last edited by pandeesh; 02-11-2012 at 12:09 PM.. Reason: typo
# 2  
Old 02-11-2012
alias mkdircd="mkdir $1 && cd $1"

Put in .profile or .bashrc of user.
# 3  
Old 02-11-2012
@Peasant: aliases do not take positional parameters, use a function instead:
Code:
mkdircd(){ mkdir "$1" && cd "$1" ;}

The double quotes are there to ensure that it also works for directories with special characters or spaces..
# 4  
Old 02-12-2012
i have modified my .profile as like this:
Code:
# ~/.profile: executed by the command interpreter for login shells.
# This file is not read by bash(1), if ~/.bash_profile or ~/.bash_login
# exists.
# see /usr/share/doc/bash/examples/startup-files for examples.
# the files are located in the bash-doc package.

# the default umask is set in /etc/profile; for setting the umask
# for ssh logins, install and configure the libpam-umask package.
#umask 022

# if running bash
if [ -n "$BASH_VERSION" ]; then
    # include .bashrc if it exists
    if [ -f "$HOME/.bashrc" ]; then
        . "$HOME/.bashrc"
    fi
fi

# set PATH so it includes user's private bin if it exists
if [ -d "$HOME/bin" ] ; then
    PATH="$HOME/bin:$PATH"
fi

. /u01/app/oracle/product/11.2.0/xe/bin/oracle_env.sh

. /u01/app/oracle/product/11.2.0/xe/bin/oracle_env.sh

mkdircd(){ mkdir "$1" && cd "$1" ;}
~

But still i am getting:
Code:
pandeeswaran@ubuntu:~$ mkdircd pandees
mkdircd: command not found

Please guide me.
Thanks
# 5  
Old 02-12-2012
Perhaps it never gets there. Have you read the second comment line of your .profile ?
Otherwise try putting an echo some text before the function definition to see if it gets there...
# 6  
Old 02-12-2012
After adding the code in .bashrc file, it works as expected:
Code:
pandeeswaran@ubuntu:~$ mkdircd pandees
pandeeswaran@ubuntu:~/pandees$

# 7  
Old 02-12-2012
Just add below in /etc/bashrc to make it public or $HOME/.bashrc for specific ..
Code:
mkdircd() { mkdir "$1" && cd "$1";}

and relogin ...

--Shirish

Moderator's Comments:
Mod Comment Please use code tags
 
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. AIX

AIX : User Defined Authorizations Question

Hello everyone, We have got a Server say Testserver with AIX 6.1 running on it. We want to create user defined authorization for syslogd, ntpd and named . We don't want to use pre-defined authorization (aix.network.daemon). Is it possible to create an independent authorization for commands?... (1 Reply)
Discussion started by: coolr
1 Replies

2. Shell Programming and Scripting

User defined functions in awk

Hi; Is der ne to to use user defined functions for the values in awk find $1 -type f -ls | nawk '{{print "|"$3"|"$5"|"$6"|"$8"|"$9"|"$10"|"} for(i=11;i<=NF;i++){printf("%s",$i)}}' In above command i want to append some values returned by user functions on line. thnks; ajay (1 Reply)
Discussion started by: ajaypadvi
1 Replies

3. Programming

add more user-defined signals

Hi Is there a way to add more user-defined signals? I am currently using SIGUSR1 and SIGUSR2 - but I need another one. How can I do that? Thanks! (9 Replies)
Discussion started by: naamabm
9 Replies

4. Solaris

Wants to use User defined Macro in Makefile

I am converting 32-bit C++ code to 64-bit on Solaris. I have used unsigned long in number of files. I want it to convert to unsigned int for 64-bit. Total files are around 2000. Can you please help me if possible to do it in makefile using MACRO while build. If it is not possible any other... (2 Replies)
Discussion started by: amit_27
2 Replies

5. Shell Programming and Scripting

need help with User Defined Function

Dear Friends, I need a help regarding User defined function in shell script. My problem is as follows: my_func.sh my_funcI(){ grep 'mystring' I.dat } my_funcQ(){ grep 'mystring' Q.dat } myfuncI myfuncQ But As both the function has same function only the... (11 Replies)
Discussion started by: user_prady
11 Replies

6. Shell Programming and Scripting

awk printf for user defined variables

I am working on a SunFire 480 - uname -a gives: SunOS bsmdb02 5.9 Generic_112233-08 sun4u sparc SUNW,Sun-Fire-480R I am tyring to sum up the total size of all the directories for each running database using awk: #!/usr/bin/ksh for Database in `ps -efl | grep "ora_pmon" | grep -v grep |... (1 Reply)
Discussion started by: jabberwocky
1 Replies

7. UNIX for Dummies Questions & Answers

User defined service

I want to add a new IP service which executes a script on SCO OS5. I have amended /etc/services and added to port number (3333) I have amended /etc/inetd.conf and added a line for this service but I can't get it to execute my own shell script When I telnet to the IP address on port 3333 I... (1 Reply)
Discussion started by: markdrury
1 Replies

8. AIX

User defined signal 1

Hi, I am just running a incremental back-up on one of my server. But these days It abrubtly fails with below error. ========== User defined signal 1 =========== When I rerun the back-up, It completed successfully.Earlier this was not happening. Any Idea, what could be the problem... (0 Replies)
Discussion started by: nitesh_raj
0 Replies

9. Shell Programming and Scripting

Nawk user-defined function

HELP!!!! I am in an on-line shell programming class and have a question. Here is the data: Mike Harrington:(510) 548-1278:250:100:175 Christian Dobbins:(408) 538-2358:155:90:201 Susan Dalsass:(206) 654-6279:250:60:50 (There are 12 contribuors total) This database contains names, phone... (1 Reply)
Discussion started by: NewbieGirl
1 Replies
Login or Register to Ask a Question