Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I'm trying to create a directory and cd into it:

In ~/.bashrc:

function abc() {
  appname=$1
  appdir="$HOME/code/$appname"
  if [ mkdir $appdir -a cd $appdir ]; then
    echo Success
  else
    echo Failed to create and switch directory
  fi
}

When I reload bashrc (. ~/.bashrc) I get the error:

bash: [: too many arguments
Failed to create and switch directory

How do I fix this? And what does [: in the error mean?

Ps. Could someone direct me to a "non-cryptic" bash scripting tutorial?

share|improve this question
    
There is something wrong in your script i guess,you can start by using proper syntax with if. if <COMMANDS>; then <COMMANDS>; fi –  karthick87 May 2 '11 at 2:45
2  
I don't know if it's just me... but try Pitfall 6; I hear it gets a few people ;) mywiki.wooledge.org/BashPitfalls –  Alex Stevens May 2 '11 at 3:24

2 Answers 2

up vote 9 down vote accepted

The main error in your script is that the [ command, equivalent to test command, is used to test conditions, like string comparison, existence of files, and so on.

To test the exit status of processes you have to use if without [, so your script could be

if mkdir "$appdir" && cd "$appdir"; then
  echo "Success"
else
  echo "Failed to create and switch directory"
fi

This is explained in Bash Pitfalls: 9. if [grep foo myfile.

I suggest you go through GrayCat Bash Guide to understand bash.

share|improve this answer
3  
+1000 for Bash Guide –  l0b0 May 2 '11 at 11:25
    
Perfect, thank you. That guide looks good. –  Zabba May 2 '11 at 16:16
    
That guide is really very good! Thank you, otherwise I would be still deciphering man pages :) –  Zabba May 2 '11 at 16:34

A prototype could be:

  • Create a file in your desktop: touch newDirectory.sh
  • Make file executable: chmod +x newDirectory.sh
  • To call the script from a terminal in the desktop : ./newDirectory.sh anyName

/

#!/bin/bash
function abc() {
  appname=${1}
  appdir="$HOME/Desktop/$appname"
  if (( mkdir "${appdir}" )) ; then     
    cd "${appdir}"  
    echo "Success" 
  else   
    echo "Failed to create and switch directory" 
  fi
}
abc ${1}

Little recommendation: if you are new, do not mess with .bashrc :)

share|improve this answer
2  
Sorry, no. There are several problems with that answer. 1) missing a {. 2) it never runs mkdir. 3) you didn't test cd's exit status. 4) Don't put extensions on scripts. Especially not .sh when it's not even an sh-script. 5) Putting it in a script defeats the purpose of the function. mywiki.wooledge.org/BashGuide –  geirha May 2 '11 at 7:38
    
@geirha: my fault, the { was there before my editing. –  enzotib May 2 '11 at 7:44
    
Answer: (( mkdir "${appdir}" )) is equal 'mkdir "${appdir}"' (look at the back ticks) –  studentz May 2 '11 at 14:29
    
debug the script bash -x newDirectory.sh and you will see that the script will cd to the directory. If you want to run another process after that, It is a completely different horse. –  studentz May 2 '11 at 14:41
    
The script run with bash dash ash. You can change the extension whatever you want. –  studentz May 2 '11 at 14:43

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.