LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
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 02-16-2009, 11:07 PM   #1
CoffeeKing!!!
Member
 
Registered: Mar 2008
Posts: 117

Rep: Reputation: Disabled
simple bash script works irregularly


I've added the following to my ~/.bashrc in order to make a directory and then change to that new directory:

Code:
mcd ()
{
    if [ "$1" = "" ]; then
        echo "mcd directory-name";
    else
        if [ ! -d $1 ]; then
            mkdir -p $1;
            cd $1;
        else
            echo "$1 directory exists";
        fi
    fi
}
however, it doesn't work perfectly and throws up errors :

Code:
mkdir: cannot create directory `swp/s': No such file or directory
bash: cd: swp/s: No such file or directory
any ideas or perhaps another way?
 
Old 02-17-2009, 12:55 AM   #2
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
Your function runs just fine for me. I can't see anything wrong with it.

The only time I get the error you do is if I run mkdir without the -p option and try to create a nested directory.
 
Old 02-17-2009, 06:58 AM   #3
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,063

Rep: Reputation: 381Reputation: 381Reputation: 381Reputation: 381
Code:
mkdir: cannot create directory `swp/s': No such file or directory
bash: cd: swp/s: No such file or directory
This usually appears when you miss -p when trying to create multiple levels in a single shot. But your script seems to use it (unless you have edited and added it afterwards).

I suggest using quotations correctly to avoid problems in the future. You don't need ; to close the lines either, unless you are concatenating many lines into a single one.

Code:
mcd ()
{
    if [ "$1" = "" ]; then
        echo "mcd directory-name"
    else
        if [ ! -d "$1" ]; then
            mkdir -p "$1"
            cd "$1"
        else
            echo "$1 directory exists"
        fi
    fi
}
 
Old 02-17-2009, 08:34 PM   #4
CoffeeKing!!!
Member
 
Registered: Mar 2008
Posts: 117

Original Poster
Rep: Reputation: Disabled
This one works too

function mkcd() {
[ -n "$1" ] && mkdir -p "$@" && cd "$1";
}
 
Old 02-18-2009, 06:30 PM   #5
servat78
Member
 
Registered: Jan 2009
Posts: 100

Rep: Reputation: 17
Could it be that you are about to work in a directory where permissions do not allow your current user to create sub-directories at all? Because your script has the needed -p flag, and it appears to make an attempt to create the directory hierarchy - missing permissions could lead to the same error.

Debian

Last edited by servat78; 02-19-2009 at 12:32 PM.
 
  


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
Simple bash script please help! Cheloo Linux - General 3 07-13-2008 04:54 PM
Simple Bash Script Filipe Linux - Newbie 2 07-17-2007 10:07 AM
need help regarding my very simple bash script hottdogg Programming 3 05-02-2007 03:09 AM
Simple bash script help zok Linux - General 4 12-15-2006 12:56 PM
Simple BASH script fio Linux - General 5 01-18-2003 04:26 PM


All times are GMT -5. The time now is 05:44 PM.

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