Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I can use help with this bit of bash I am trying to write. The scripts purpose is to speed up my development while working with multiple projects. I labeled the parts that I have questions about in the code.

# is there a way to persist this through working enviornments besides this?
declare -x WORKING=`cat ~/.working`

#alias p='builtin cd $WORKING && pwd && ls'
alias pj='builtin cd $WORKING/public/javascripts && pwd && ls'

function pp {
echo `pwd` > ~/.working
}


# is there a way to close the scope of this function?
function p {

  # how do I process flags here?
  # -f and -d etc. can exist but may not
  # either way I want $1 to be set to the first string if there
  # is one


  if [ -z "$1" ]
  then
    echo '*'
    builtin cd $WORKING && pwd && ls
    return
  fi



  BACK=`pwd`
  builtin cd $WORKING

  #f=`find . -iname "$1"`
  f=( `echo $(find . -type d -o -type f -iname "$1") | grep -v -E "git|node"` )
  #echo ${f[1]}

  if [ -z "${f[0]}" ]
  then
    return
  fi


  if [ -z "${f[1]}" ]
  then
    # how can I write this as a switch?
    if [ -f ${f[0]} ]
    then
      vim ${f[0]}
      return
    fi
    if [ -d ${f[0]} ]
    then
      builtin cd ${f[0]}
      return
    fi
  else
    echo "multiple found"
  #for path in $f
  #do
  # sort files and dirs
  #  sort dirs by path
  #  sort files by path
    #done

  #  display dirs one color
  #  display files another color
  #     offer choices
  #     1) open all files
  #     2) open a file
  #     3) cd to selected directory
  #     4) do nothing

  fi


 # nothing found
 builtin $BACK
}
share|improve this question
add comment

2 Answers 2

# is there a way to persist this through working enviornments besides this?
declare -x WORKING=`cat ~/.working`

Perhaps use:

export WORKING=$(cat ~/.working)

This should add it to your enviornment till reboot.

You should be able to reference this later on by using

echo $WORKING

from prompt.

share|improve this answer
    
good call! assuring i did it the same way thank you –  James Andino Nov 3 '12 at 15:55
    
You could also add export WORKING=$HOME/.working into your ~/.bashrc –  sparticvs Nov 3 '12 at 18:45
add comment

For variable persistance, you need a mechanism that's not main memory. Files are a good choice. Here I use a bash shortcut for $(cat filename)

declare -x WORKING=$(< ~/.working)

You don't need to echo $(pwd), just pwd

function pp { pwd > ~/.working; } 

By "close the scope", I assume you mean keep local variables local to the function: use the local builtin

function p {

  local OPTIND OPTARG
  local optstring=':fd'  # declare other options here: see "help getopts"
  local has_f=false has_d=false

  while getopts $optstring option; do
    case $option in
      f) has_f=true ;;
      d) has_d=true ;;
      ?) echo "invalid option: -$OPTARG"; return 1 ;;
    esac
  done
  shift $((OPTIND - 1))

  if $has_f ; then
    do something if -f

  elif $has_d ; then
    do something if -d
  fi

  # ... whatever else you have to do ...
}
share|improve this answer
add comment

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.