cd.bash 912 B

123456789101112131415161718192021222324252627282930313233
  1. #!/bin/bash
  2. # the variable _ is the command that bash is executing now
  3. # $1 is the first argument to the command running now
  4. # $2 is the second argument to the command running now
  5. # echo ${_:5} will print the string from _ at position 5 to the end of the variable
  6. #if the directory the user entered is a valid directory,
  7. #then change to that directory
  8. if [ -d $1 ]
  9. then
  10. echo "$1 is a valid directory"
  11. echo "executing cd $1"
  12. cd $1
  13. echo "$(pwd)"
  14. #otherwise, save the current working directory
  15. #then cd to ~/, and test if the directory that the user entered,
  16. #is located at ~/ If so, change to that directory. If not,
  17. # then change to the current directory
  18. else
  19. echo 'you did not enter a valid directory...checking ~/'
  20. current_directory=$(pwd)
  21. cd /home/joshua/
  22. if [ -d $1 ]
  23. then
  24. cd $1
  25. else
  26. echo "$1 not a valid directory in ~/ either"
  27. cd $current_directory
  28. fi
  29. fi