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


Topics about command line

Command line: variables from command line


		  
		  

Tasks:
1.1. print the array of the argments 
2.2. print individual arguments
3.3. loop over the arguments
4.4. print the script name
5.5. print the script path
6.6. print the number of arguments
		  
Bash Perl Python
  1. cat>/tmp/tst <<"EOF"
  2. #1.1. print the array of the argments
  3. echo "$@"
  4. echo "$*"
  5. #2.2. print individual arguments
  6. echo $1 $2 $3
  7. echo "the last argument: ${!#}"
  8. #3.3. loop over the arguments
  9. for i in "$@";do echo $i;done
  10. #4.4. print the script name
  11. echo $0
  12. #5.5. print the script path
  13. echo `readlink -f $0`
  14. #6.6. print the number of arguments
  15. echo $#
  16. EOF
  17. bash /tmp/tst arg1 arg2 arg3
  18. -------output----------
  1. arg1 arg2 arg3
  2. arg1 arg2 arg3
  3. arg1 arg2 arg3
  4. the last argument: arg3
  5. arg1
  6. arg2
  7. arg3
  8. /tmp/tst
  9. /tmp/tst
  10. 3
  1. # or cat>/tmp/tst<<<'print "@ARGV\n"';
  2. cat>/tmp/tst<<'EOF'
  3. print "@ARGV\n";
  4. print "$ARGV[0] $ARGV[1] $ARGV[2]\n";
  5. print "$_\n" foreach(@ARGV);
  6. print "$0\n";
  7. print ((0+@ARGV),"\n");
  8. print scalar(@ARGV), "\n";
  9. EOF
  10. perl /tmp/tst arg1 arg2 arg3
  11. -------output----------
  1. arg1 arg2 arg3
  2. arg1 arg2 arg3
  3. arg1
  4. arg2
  5. arg3
  6. /tmp/tst
  7. 3
  8. 3
  1. # or echo -e "import sys\nprint str(sys.argv)" > /tmp/tst
  2. cat >/tmp/tst<<'EOF'
  3. import sys
  4. import os
  5. #1.1. print the array of the argments
  6. print str(sys.argv);
  7. #2.2. print individual arguments
  8. print sys.argv[1],sys.argv[2],sys.argv[3]
  9. #5.5. print the script path
  10. print os.path.basename(__file__)
  11. print sys.path[0]
  12. EOF
  13. python /tmp/tst arg1 arg2 arg3
  14. -------output----------
  1. ['/tmp/tst', 'arg1', 'arg2', 'arg3']
  2. arg1 arg2 arg3
  3. tst
  4. /tmp

Command line: use getopt(s) to handle the command line


		  
		  

Tasks:
1.1. ceate a script file to handle commandline with getopt(s)
2.2. run the script with some arguments
		  
Bash Perl Python
  1. # getopts
  2. cat>/tmp/tar.sh<<"EOF"
  3. declare -A myopts
  4. usage() {
  5. echo "Usage: $0 [-zxvf <fname>] [-C <dir>]" 1>&2
  6. exit 1
  7. }
  8. while getopts "xvzf:C:" opt "$@"; do
  9. case "${opt}" in
  10. z|x|v|C|f) myopts["${opt}"]=${OPTARG} ;;
  11. *) usage ;;
  12. esac
  13. done
  14. shift $((OPTIND-1))
  15. for key in ${!myopts[@]};
  16. do echo "$key => ${myopts[$key]}"; done
  17. EOF
  18. bash /tmp/tar.sh -zxvf ~//tmp/tst.tar.gz -C /tmp
  19. -------output----------
  1. C => /tmp
  2. f => /home/neutrino0717//tmp/tst.tar.gz
  3. v =>
  4. x =>
  5. z =>
  1. # getopts
  2. cat>/tmp/tar.pl<<"EOF"
  3. use Data::Dumper;
  4. use Getopt::Std;
  5. # read options
  6. getopts('C:zxvf:', \%opts);
  7. print "the options are:\n";
  8. print Dumper (\%opts);
  9. EOF
  10. perl /tmp/tar.pl -zxvf ~//tmp/tst.tar.gz -C /tmp
  11. -------output----------
  1. the options are:
  2. $VAR1 = {
  3. 'z' => 1,
  4. 'C' => '/tmp',
  5. 'v' => 1,
  6. 'x' => 1,
  7. 'f' => '/home/neutrino0717//tmp/tst.tar.gz'
  8. };
  1. # getopt
  2. cat>/tmp/tar.py<<"EOF"
  3. import sys, getopt
  4. argv=sys.argv[1:]
  5. try:
  6. opts = getopt.getopt(argv,"zxvf:C:")
  7. except getopt.GetoptError:
  8. print "Usage: "+sys.argv[0]+" [-zxvf <fname>] [-C <dir>]"
  9. sys.exit(2)
  10. print opts
  11. EOF
  12. python /tmp/tar.py -zxvf ~/tmp/tst.tar.gz -C /tmp
  13. -------output----------
  1. ([('-z', ''), ('-x', ''), ('-v', ''), ('-f', '/home/neutrino0717/tmp/tst.tar.gz'), ('-C', '/tmp')], [])