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


Topics about special variables

Special variables: command line related variables

In perl, use 'man perlvar'command to have a look of the predefined variables
In python, it is called 'magic names'

Tasks:
1.1. create the script file named /tmp/tst
2.2. invoke the script file to test special variables
3.3. create a function named func1
4.3. invoke the function to test the special variables
		  
Bash Perl Python
  1. #1.1. create the script file named /tmp/tst
  2. cat>/tmp/tst <<"EOD"
  3. echo "\$0 returns program name: $0"
  4. echo "\$1 returns the 1st argument: $1"
  5. echo "\$* returns the argument list: $*"
  6. echo "\$@ returns the argument list: $@"
  7. echo "\$$ returns the shell PID: $$"
  8. echo "\$_ returns the lastest command: $_"
  9. echo "\${#@} returns the argument number:${#@}"
  10. echo "\${!#} returns the last argument: ${!#}"
  11. echo "\$# returns the argument num: $#"
  12. echo "\$- returns the shell options list:$-"
  13. echo "\$OPTARG returns the next element of argv:$OPTARG"
  14. echo "\$OPTIND returns the next index of argv: $OPTIND"
  15. EOD
  16. #2.2. invoke the script file to test special variables
  17. bash /tmp/tst a{1..3}
  18. #3.3. create a function named func1
  19. func1(){
  20. echo "\$0 returns program name: $0"
  21. echo "\$1 returns the 1st argument: $1"
  22. echo "\$* returns the argument list: $*"
  23. echo "\$@ returns the argument list: $@"
  24. echo "\$$ returns the shell PID: $$"
  25. echo "\$_ returns the lastest command: $_"
  26. echo "\${#@} returns the argument number:${#@}"
  27. echo "\${!#} returns the last argument: ${!#}"
  28. echo "\$# returns the argument num: $#"
  29. return 0 #zero is right,function will be called as subshell
  30. }
  31. #4.3. invoke the function to test the special variables
  32. func1 p{1..4} #or func p1 p2 p3 p4
  33. echo "the return value is $?"
  34. -------output----------
  1. #1.1. create the script file named /tmp/tst
  2. #2.2. invoke the script file to test special variables
  3. $0 returns program name: /tmp/tst
  4. $1 returns the 1st argument: a1
  5. $* returns the argument list: a1 a2 a3
  6. $@ returns the argument list: a1 a2 a3
  7. $$ returns the shell PID: 18805
  8. $_ returns the lastest command: $$ returns the shell PID: 18805
  9. ${#@} returns the argument number:3
  10. ${!#} returns the last argument: a3
  11. $# returns the argument num: 3
  12. $- returns the shell options list:hB
  13. $OPTARG returns the next element of argv:
  14. $OPTIND returns the next index of argv: 1
  15. #3.3. create a function named func1
  16. #4.3. invoke the function to test the special variables
  17. $0 returns program name: bash
  18. $1 returns the 1st argument: p1
  19. $* returns the argument list: p1 p2 p3 p4
  20. $@ returns the argument list: p1 p2 p3 p4
  21. $$ returns the shell PID: 18803
  22. $_ returns the lastest command: $$ returns the shell PID: 18803
  23. ${#@} returns the argument number:4
  24. ${!#} returns the last argument: p4
  25. $# returns the argument num: 4
  26. the return value is 0
  1. #1.1. create the script file named /tmp/tst
  2. cat>/tmp/tst <<'EOF'
  3. use feature ":5.10";
  4. say "the script name :\$0 -->$0";
  5. say "the argument list:\@ARGV -->@ARGV";
  6. say "the argument 1st :\$ARGV[0] -->$ARGV[0]";
  7. say "the argument num :\scalar(\@ARGV) -->".scalar(@ARGV);
  8. say "the argument num :0+\@ARGV -->".(0+@ARGV);
  9. say "the argument num :1+\$#ARGV -->".(1+$#ARGV);
  10. EOF
  11. #2.2. invoke the script file to test special variables
  12. perl /tmp/tst arg1 arg2 arg3
  13. -------output----------
  1. the script name :$0 -->/tmp/tst
  2. the argument list:@ARGV -->arg1 arg2 arg3
  3. the argument 1st :$ARGV[0] -->arg1
  4. the argument num :scalar(@ARGV) -->3
  5. the argument num :0+@ARGV -->3
  6. the argument num :1+$#ARGV -->3
  1. #1.1. create the script file named /tmp/tst
  2. cat >/tmp/tst<<'EOF'
  3. import sys
  4. import os
  5. print "the argument array: sys.argv -->", sys.argv
  6. print "the script name: sys.argv[0] -->", sys.argv[0]
  7. print "the script name: __file__ -->", __file__
  8. print "the array of path: sys.path -->", sys.path[0]
  9. print "the script path: sys.path[0] -->", sys.path[0]
  10. EOF
  11. #2.2. invoke the script file to test special variables
  12. python /tmp/tst arg1 arg2 arg3
  13. -------output----------
  1. the argument array: sys.argv --> ['/tmp/tst', 'arg1', 'arg2', 'arg3']
  2. the script name: sys.argv[0] --> /tmp/tst
  3. the script name: __file__ --> /tmp/tst
  4. the array of path: sys.path --> /tmp
  5. the script path: sys.path[0] --> /tmp

Special variables: user environment variables


		  
		  

Tasks:
1.1. Copy and paste all the content before the '---output--' part in linux bash prompt:
		  
Bash Perl Python
  1. #1.1. Copy and paste all the content before the '---output--
  1. ' part in linux bash prompt:
  2. bash <<"EOF"
  3. echo "the login name: \$LOGNAME -->$LOGNAME"
  4. echo "the current working dir: \$PWD -->$PWD"
  5. echo "the user default working dir: \$HOME -->$HOME"
  6. echo "the users mail file full path: \$MAIL -->$MAIL"
  7. echo "the current shell: \$SHELL -->$SHELL"
  8. echo "the shell search path: \$PATH -->$PATH"
  9. echo "the default locale: \$LANG -->$LANG"
  10. echo "the current terminal type: \$TERM -->$TERM"
  11. echo "the primary system prompt: \$PS1 -->$PS1"
  12. echo "the secondary system prompt: \$PS2 -->$PS2"
  13. echo "the Internal field separator: \$IFS -->$IFS"
  14. # echo "the files colour: \$LS_COLORS -->$LS_COLORS"
  15. EOF
  1. #1.1. Copy and paste all the content before the '---output--
  1. ' part in linux bash prompt:
  2. # paste below in bash shell
  3. script=$(cat <<'EOF'
  4. use feature ":5.10";
  5. say "\$ENV{'LOGNAME'} -->$ENV{'LOGNAME'}";
  6. say "\$ENV{'PWD'} -->$ENV{'PWD'}";
  7. EOF
  8. )
  9. # run the script in bash shell
  10. perl -e "$script"
  1. #1.1. Copy and paste all the content before the '---output--
  1. ' part in linux bash prompt:
  2. # paste below in bash shell
  3. python <<'EOF'
  4. import os
  5. print "os.getenv('LOGNAME')-->"+os.getenv('LOGNAME')
  6. print "os.environ['PWD']-->"+os.environ['PWD']
  7. # print all, commented for too many output
  8. # for key in os.environ.keys():
  9. # print "%s %s" %(key,os.environ[key])
  10. EOF

Special variables: process related variables


		  
		  

Tasks:
1.1. print out process related variables
		  
Bash Perl Python
  1. bash <<"EOF"
  2. echo "current bash PID: \$\$--> $$"
  3. pgrep "$0"
  4. echo "parent PID of the bash: \$PPID --> $PPID"
  5. sleep 1 &
  6. echo "last background process PID: \$! --> $!"
  7. cat /etc/noexists 2>/dev/null
  8. echo "exit status of the last command: \$? --> $?"
  9. EOF
  10. -------output----------
  1. current bash PID: $$--> 18811
  2. 18811
  3. 29861
  4. 31291
  5. 32127
  6. 56685
  7. 64466
  8. 64734
  9. parent PID of the bash: $PPID --> 18810
  10. last background process PID: $! --> 18813
  11. exit status of the last command: $? --> 1
  1. #1.1. print out process related variables
  2. # paste below in bash shell
  3. script=$(cat <<'EOF'
  4. use feature ":5.10";
  5. say "current script PID: \$\$ -->$$";
  6. system "cat /etc/noexists 2>/dev/null";
  7. say "system call exit status: \$? -->$?";
  8. open(FH,'<',"/etc/noexists") or
  9. say "error message: \$! -->$!";
  10. foreach (@ARGV) {
  11. say "default arg in loop or grep:\$_ --> $_";
  12. }
  13. sub func1 {
  14. say "subroutine argument list: \@_ --> @_";
  15. }
  16. func1(@ARGV);
  17. func1("p1", "p2", "p3");
  18. EOF
  19. )
  20. # run the script in bash shell
  21. perl -e "$script" arg1 arg2 arg3
  22. -------output----------
  1. current script PID: $$ -->18816
  2. system call exit status: $? -->256
  3. error message: $! -->No such file or directory
  4. default arg in loop or grep:$_ --> arg1
  5. default arg in loop or grep:$_ --> arg2
  6. default arg in loop or grep:$_ --> arg3
  7. subroutine argument list: @_ --> arg1 arg2 arg3
  8. subroutine argument list: @_ --> p1 p2 p3
  1. # todo
  2. -------output----------
  1. # todo