Coding Bash vs. Perl vs. Python (by Evan Liu)


Topics about path

Path: get the dirname of a full path


		  
		  

Tasks:
1.1. define a variable with value of a file's full path
2.2. print the directory of the file
		  
Bash Perl Python
  1. #1.1. define a variable with value of a file's full path
  2. myfile="/home/remote/5th Jun/bash perl python.txt"
  3. #2.2. print the directory of the file
  4. echo "`dirname "$myfile"`"
  5. # another way
  6. echo "${myfile%/*}"
  7. # the 3rd way, use regular expression
  8. sed "s/\(.*\)\/.*/\1/" <<<"$myfile"
  9. -------output----------
  1. #1.1. define a variable with value of a file's full path
  2. #2.2. print the directory of the file
  3. /home/remote/5th Jun
  4. # another way
  5. /home/remote/5th Jun
  6. # the 3rd way, use regular expression
  7. /home/remote/5th Jun
  1. use feature ":5.10" ;
  2. use Cwd qw(abs_path cwd);
  3. use File::Basename;
  4. #1.1. define a variable with value of a file's full path
  5. $mypath="/home/remote/5th Jun/bash perl python.txt";
  6. #2.2. print the directory of the file
  7. print dirname($mypath)."\n";
  8. # another way, use regex
  9. $mypath =~ s/(.*)\/.*/\1/;
  10. print $mypath;
  11. -------output----------
  1. #1.1. define a variable with value of a file's full path
  2. #2.2. print the directory of the file
  3. /home/remote/5th Jun
  4. # another way, use regex
  5. /home/remote/5th Jun
  1. import os
  2. #1.1. define a variable with value of a file's full path
  3. mypath="/home/remote/5th Jun/bash perl python.txt"
  4. #2.2. print the directory of the file
  5. print os.path.dirname(os.path.abspath(mypath))
  6. # or use os.path.split
  7. dname, fname = os.path.split(mypath)
  8. print dname
  9. # or use regex
  10. import re
  11. print re.sub(r'(.*)\/.*', r'\1', mypath, flags=re.M)
  12. -------output----------
  1. #1.1. define a variable with value of a file's full path
  2. #2.2. print the directory of the file
  3. /home/remote/5th Jun
  4. # or use os.path.split
  5. /home/remote/5th Jun
  6. # or use regex
  7. /home/remote/5th Jun

Path: get the basename of a full path


		  
		  

Tasks:
1.1. define a variable with value of a file's full path
2.2. print the filename of the file
		  
Bash Perl Python
  1. #1.1. define a variable with value of a file's full path
  2. myfile="/home/remote/5th Jun/bash perl python.txt"
  3. #2.2. print the filename of the file
  4. echo "`basename "$myfile"`"
  5. echo "`basename "$myfile" ".txt"`"
  6. # or
  7. echo "${myfile##*/}"
  8. # or use awk
  9. awk -F/ '{print $NF}' <<<"$myfile"
  10. # or use sed
  11. sed "s/.*\///" <<<"$myfile"
  12. sed "s/.*\/\(.*\)/\1/" <<<"$myfile"
  13. # or use rev and cut
  14. rev <<<"$myfile" |cut -d"/" -f 1 |rev
  15. -------output----------
  1. #1.1. define a variable with value of a file's full path
  2. #2.2. print the filename of the file
  3. bash perl python.txt
  4. bash perl python
  5. # or
  6. bash perl python.txt
  7. # or use awk
  8. bash perl python.txt
  9. # or use sed
  10. bash perl python.txt
  11. bash perl python.txt
  12. # or use rev and cut
  13. bash perl python.txt
  1. use feature ":5.10" ;
  2. use Cwd qw(abs_path cwd);
  3. use File::Basename;
  4. #1.1. define a variable with value of a file's full path
  5. $mypath="/home/remote/5th Jun/bash perl python.txt";
  6. #2.2. print the filename of the file
  7. print basename($mypath);
  8. -------output----------
  1. #1.1. define a variable with value of a file's full path
  2. #2.2. print the filename of the file
  3. bash perl python.txt
  1. import os
  2. mypath="/home/remote/5th Jun/bash perl python.txt"
  3. #1.1. define a variable with value of a file's full path
  4. dname, fname = os.path.split(mypath)
  5. print fname
  6. #2.2. print the filename of the file
  7. print os.path.basename(mypath)
  8. -------output----------
  1. #1.1. define a variable with value of a file's full path
  2. bash perl python.txt
  3. #2.2. print the filename of the file
  4. bash perl python.txt

Path: get the path of current script

1. create the script file.
2. run the script file to get it's path

Tasks:

		  
Bash Perl Python
  1. # 1.
  2. cat >/tmp/test.sh<<"EOD"
  3. # the perfect way
  4. (cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd)
  5. # wrong if you use: source scriptname [arguments]
  6. (cd "$( dirname "$0" )" && pwd)
  7. EOD
  8. # 2.
  9. bash /tmp/test.sh
  10. source /tmp/test.sh 2>&1
  11. -------output----------
  1. # 1.
  2. # 2.
  3. # the perfect way
  4. /tmp
  5. # wrong if you use: source scriptname [arguments]
  6. /tmp
  7. # the perfect way
  8. /tmp
  9. # wrong if you use: source scriptname [arguments]
  10. /home/neutrino0717/bash4u
  1. # 1.
  2. cat >/tmp/test.pl<<"EOF"
  3. use Cwd 'abs_path';
  4. print abs_path($0);
  5. EOF
  6. # 2.
  7. perl /tmp/test.pl
  8. -------output----------
  1. /tmp/test.pl
  1. # 1.
  2. cat >/tmp/test.py<<"EOF"
  3. import os
  4. print os.path.dirname(os.path.realpath(__file__))
  5. print os.path.dirname(os.path.abspath(__file__))
  6. EOF
  7. # 2.
  8. python /tmp/test.py
  9. -------output----------
  1. /tmp
  2. /tmp

Path: get the current working dir

In bash:
A good practice is to use subshell,and thus will not affect the working directory of parent shell.
alias new_job='(cd /boot; pwd; echo "in subshell: $$ $BASHPID. working dir: `pwd`")'

Tasks:

		  
Bash Perl Python
  1. echo `pwd`
  2. -------output----------
  1. /home/neutrino0717/bash4u
  1. #get the currnet working directory
  2. use Cwd;
  3. print getcwd();
  4. -------output----------
  1. #get the currnet working directory
  2. /home/neutrino0717/bash4u
  1. import os
  2. print os.getcwd()
  3. -------output----------
  1. /home/neutrino0717/bash4u

Path: how to change working dir

In bash:
If you are running bash v4 or better, the PID of the subshell is available in $BASHPID.

In python:
The best way is to use File::chdir and $CWD variable
Using below maybe problematic in case of exeptions:
  my $cwd_orig = cwd;
  chdir "$cwd_new";
  #do some work...
  chdir $cwd_origin;

Tasks:

		  
Bash Perl Python
  1. #start subshell
  2. cd /bin
  3. echo "In parshell: $$ $BASHPID. working dir: `pwd`"
  4. (cd /home; echo "in subshell: $$ $BASHPID. working dir: `pwd`")
  5. echo "In parshell: $$ $BASHPID. working dir: `pwd`"
  6. -------output----------
  1. #start subshell
  2. In parshell: 18964 18964. working dir: /bin
  3. in subshell: 18964 18966. working dir: /home
  4. In parshell: 18964 18964. working dir: /bin
  1. #
  2. use Cwd;
  3. use File::chdir;
  4. chdir "/media";
  5. print "in main, working dir: ".getcwd()."\n";
  6. $CWD = '/bin';
  7. print "in main, working dir: ".getcwd()."\n";
  8. {
  9. local $CWD = "/home";
  10. print "in {}, working dir: ".getcwd()."\n";
  11. }
  12. print "back to main, working dir: ".getcwd()."\n";
  13. -------output----------
  1. #
  2. in main, working dir: /media
  3. in main, working dir: /bin
  4. in {}, working dir: /home
  5. back to main, working dir: /bin
  1. #todo
  2. -------output----------
  1. #todo