Tuesday, May 3, 2011

Unable to convert Bash script to Zsh script

I need to change the following Bash code to Zsh

TODO_OPTIONS="--timeout --summary"
         cd ()
         {   
             builtin cd "$@"
             RV=$?
             [ $RV = 0 -a -r .todo ] && devtodo ${TODO_OPTIONS}
             return $RV 
         }   

         pushd ()
         {   
             builtin pushd "$@"
             RV=$?
             [ $RV = 0 -a -r .todo ] && devtodo ${TODO_OPTIONS}
             return $RV 
         }   

         popd ()
         {   
             builtin popd "$@"
             RV=$?   
             [ $RV = 0 -a -r .todo ] && devtodo ${TODO_OPTIONS}
             return $RV 
         }   

         # Run todo initially upon login
         devtodo ${TODO_OPTIONS}

I get the following error when I start Zsh with the code

todo: error, unknown argument '--timeout --summary', try --help

I feel that Zsh cannot understand the following line

[ $RV = 0 -a -r .todo ] && devtodo ${TODO_OPTIONS}

Other commands in the first code seems to be right for Zsh.

How can you convert the code to Zsh?

From stackoverflow
  • You're saving the text as one string/object, instead of as a simple "thing to substitute". You can either save the string properly:

    TODO_OPTIONS=(--timeout --summary)
    ....
    devtodo ${TODO_OPTIONS}
    

    Or run word splitting on your variable:

    TODO_OPTIONS="--timeout --summary"
    ....
    devtodo ${=TODO_OPTIONS}
    
    Masi : @viraptor: Thank you for your answer!
    Masi : The first version of your codes works. :)

0 comments:

Post a Comment

Note: Only a member of this blog may post a comment.