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


Topics about input

Input: Read input from console

To get input from console:
In Bash, use 'read' command, it's the only reliable way as far as I know.
In Perl, use '<>' or '<STDIN>' to get console input.
In python, you can use 'raw_input' function.

Tasks:
1.1. Read input from user with prompt "Your name:"
2.2. Print a greeting message to console
		  
Bash Perl Python
  1. # To really input from console, just remove the '<<<"Evan Liu"'
  2. if read -p "Your name:" x; then
  3. echo "Hello, $x."
  4. fi <<<"Evan Liu"
  5. -------output----------
  1. # To really input from console, just remove the '<<<Evan Liu'
  2. Hello, Evan Liu.
  1. #'$x=<>' is the same as '$x=<STDIN>'
  2. print "Your name:";
  3. if($x=<>){
  4. print "Hello, $x";
  5. }
  6. ---------------output-----------------
  1. Your name: Evan Liu
  2. Hello, Evan Liu
  1. x=raw_input("Your name: ")
  2. print "Hello, " + x
  3. ----------------output-------------
  1. Your name: Evan Liu
  2. Hello, Evan Liu

Input: Read input from file line by line

Here in Bash, I use one liners to make the codes concise.
In Perl and Python, Glob function can get all the files that match the pattern, actually we don't need glob in this example,  just to show how to use it.

Tasks:
1.1. Read from a file line by line, then print each line with an arrow in front.
		  
Bash Perl Python
  1. while read l; do echo "-->$l";done <<< "`cat /etc/lsb-release`"
  2. # or
  3. while read l; do echo "-->$l"; done < '/etc/lsb-release'
  4. -------output----------
  1. -->DISTRIB_ID=Ubuntu
  2. -->DISTRIB_RELEASE=14.04
  3. -->DISTRIB_CODENAME=trusty
  4. -->DISTRIB_DESCRIPTION="Ubuntu 14.04.5 LTS"
  5. # or
  6. -->DISTRIB_ID=Ubuntu
  7. -->DISTRIB_RELEASE=14.04
  8. -->DISTRIB_CODENAME=trusty
  9. -->DISTRIB_DESCRIPTION="Ubuntu 14.04.5 LTS"
  1. open FH,"<","/etc/lsb-*";
  2. print "--> $_" while(<FH>);
  3. # or
  4. open(FH, glob("/etc/lsb-*"));
  5. print "--> $_" while(<FH>);
  6. -------output----------
  1. # or
  2. --> DISTRIB_ID=Ubuntu
  3. --> DISTRIB_RELEASE=14.04
  4. --> DISTRIB_CODENAME=trusty
  5. --> DISTRIB_DESCRIPTION="Ubuntu 14.04.5 LTS"
  1. import glob
  2. for file in glob.glob("/etc/lsb-*"):
  3. fob = open(file,'r')
  4. print fob.read()
  5. fob.close()
  6. -------output----------
  1. DISTRIB_ID=Ubuntu
  2. DISTRIB_RELEASE=14.04
  3. DISTRIB_CODENAME=trusty
  4. DISTRIB_DESCRIPTION="Ubuntu 14.04.5 LTS"

Input: Read lines of a file into array

#Bash
IFS means internal field separator, its' default value is <space>, <tab> and <newline>. For the file need to be separated by newline, we will set it to IFS=$'\r\n'.  Thew following are some other IFS examples:
IFS=: elems=($PATH) #read $PATH into array
IFS=. read -r i1 i2 i3 i4 <<<"192.168.7.17"  #break IP address  into variables.

#Perl
<> is called diamond operator, which is  used when a program is expecting input.  the file content or Stdin are sent through <>

#Python 
Use the built in function open() , which returns the File Object. then call the readlines() member function to return the Array.

Tasks:
1.1. Read the file into an array, with each line as an element.
2.2. Print out all array elements
		  
Bash Perl Python
  1. #1.1. Read the file into an array, with each line as an element.
  2. IFS=$'\r\n';
  3. # arr=(`cat /etc/lsb-release`)
  4. arr=(`< /etc/lsb-release`)
  5. #2.2. Print out all array elements
  6. for t in "${arr[@]}";do echo $t;done
  7. unset IFS
  8. -------output----------
  1. #1.1. Read the file into an array, with each line as an element.
  2. # arr=(DISTRIB_ID=Ubuntu
  3. DISTRIB_RELEASE=14.04
  4. DISTRIB_CODENAME=trusty
  5. DISTRIB_DESCRIPTION="Ubuntu 14.04.5 LTS")
  6. #2.2. Print out all array elements
  7. DISTRIB_ID=Ubuntu
  8. DISTRIB_RELEASE=14.04
  9. DISTRIB_CODENAME=trusty
  10. DISTRIB_DESCRIPTION="Ubuntu 14.04.5 LTS"
  1. #1.1. Read the file into an array, with each line as an element.
  2. open ($fh, glob("/etc/lsb-*"));
  3. @lines = <$fh>;
  4. close $fh;
  5. #2.2. Print out all array elements
  6. print @lines;
  7. # or use one liner
  8. print do { open ($fh, glob("/etc/lsb-*")); <$fh>;};
  9. # or use File::Slurp
  10. # use File::Slurp;
  11. # @lines = read_file(glob('/etc/lsb-*'));
  12. -------output----------
  1. #1.1. Read the file into an array, with each line as an element.
  2. #2.2. Print out all array elements
  3. DISTRIB_ID=Ubuntu
  4. DISTRIB_RELEASE=14.04
  5. DISTRIB_CODENAME=trusty
  6. DISTRIB_DESCRIPTION="Ubuntu 14.04.5 LTS"
  7. # or use one liner
  8. DISTRIB_ID=Ubuntu
  9. DISTRIB_RELEASE=14.04
  10. DISTRIB_CODENAME=trusty
  11. DISTRIB_DESCRIPTION="Ubuntu 14.04.5 LTS"
  12. # or use File::Slurp
  13. # use File::Slurp;
  14. # DISTRIB_ID=Ubuntu
  15. DISTRIB_RELEASE=14.04
  16. DISTRIB_CODENAME=trusty
  17. DISTRIB_DESCRIPTION="Ubuntu 14.04.5 LTS"
  18. = read_file(glob('/etc/lsb-*'));
  1. import glob
  2. fob = open("/etc/lsb-release",'r')
  3. arr = fob.readlines()
  4. for i in arr: print i,
  5. fob.close()
  6. -------output----------
  1. DISTRIB_ID=Ubuntu
  2. DISTRIB_RELEASE=14.04
  3. DISTRIB_CODENAME=trusty
  4. DISTRIB_DESCRIPTION="Ubuntu 14.04.5 LTS"