Tuesday, 6 December 2016

Shell Script 6

Final version cpdate
#!/bin/bash

# This is copy a file and appending the date and  time to the end of the file.
# Not the following format is wrong, because you can't add the space between the
# varible name and = symbol.
#date_formatted =$(date +%m_%d_%y-%H.%M.%S)
echo "This is the date and Time: " $date_formatted

date_formatted=$(date +%Y-%m-%d_%H.%M%S)
file_extension=$(echo "$1" | awk -F . '{print $NF}')
file_name=$(basename $1 .$file_extension)
cp -iv $1 $file_name-$date_formatted.$file_extension
~

Monday, 5 December 2016

Shell Basics 5

If you want to hand file names that contain space, you need to double quote the paramters.

#!/bin/bash

# This is copy a file and appending the date and  time to the end of the file.
# Not the following format is wrong, because you can't add the space between the
# varible name and = symbol.
#date_formatted =$(date +%m_%d_%y-%H.%M.%S)
date_formatted=$(date +%m_%d_%y-%H.%M.%S)
echo "This is the date and Time: " $date_formatted
#cp -iv $1 $2.$date_formatted      # This command does not handle the file name that
                                                      # contains space
cp -iv "$1" "$2".$date_formatted # This can handle file name contains space

Sunday, 4 December 2016

Shell Script Basics 4: compare two strings


This post explains the if-then-else usage of shell script with an example. Please note that the square bracket is equivalent with the 'test' command.

~/bin$ cat testingifthenelse
#!/bin/bash
#if test $1 = $2
if [ $1 = $2 ] # this format is equivalent with the above 'test' format
  then
  echo "The arguments are equal."
else
  echo "The arguments are NOT equal."
fi

I don't know whether the readers have noticed that  we use '=' to compare the two the two string $1 and $2, this is a different syntax with C/C++, which use '==' to compare strings. DO NOT confuse between them.





Saturday, 3 December 2016

Shell Script Basics 3

jtony@genoa:~/bin$ cat datecp
#!/bin/bash

# This is copy a file and appending the date and  time to the end of the file.
# Not the following format is wrong, because you can't add the space between the
# varible name and = symbol.
#date_formatted =$(date +%m_%d_%y-%H.%M.%S)
date_formatted=$(date +%m_%d_%y-%H.%M.%S)
echo "This is the date and Time: " $date_formatted
cp -iv $1 $2.$date_formatted

Friday, 2 December 2016

Shell Script Basics 2

jtony@genoa:~/bin$ cat hello.sh
#!/bin/bash

# This is copy a file and appending the date and  time to the end of the file.
# Not the following format is wrong, because you can't add the space between the
# varible name and = symbol.
#date_formatted =$(date +%m_%d_%y-%H.%M.%S)
date_formatted=$(date +%m_%d_%y-%H.%M.%S)
echo "This is the date and Time: " $date_formatted

Thursday, 1 December 2016

How to use C++11 range based for loop correctly

Without deep understanding of C++11 range based for loop, it is easy to make stupid mistakes which is hard to understand reason by reading long and complex compiler error message. Here I list what is the right and wrong format to use C++11 range based for loop.

#include <iostream>
#include <map>
#include <string>
using namespace std;

int main()

{
  map<int, string> mapStudent;

  mapStudent.insert(pair<int, string>(1, "student_one"));

  mapStudent.insert(pair<int, string>(2, "student_two"));

  mapStudent.insert(pair<int, string>(3, "student_three"));

  map<int, string>::iterator iter;

  // Correct iterator usage before C++11
  for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)
  {
    cout << iter->first << " " << iter->second << endl;
  }
  
  // Wrong range based for loop usage, mix up iterator and range based for loop element
  //for (iter : mapStudent) {
  //  cout << iter->first << " " << iter->second << endl;
  //}






  
// Correct range based for loop usage, auto deduct element type
  for (auto &elem : mapStudent) {
    cout << elem.first << " " << elem.second << endl;
  }
// Correct range based for loop usage, explicitly tell the compiler the element type
  for (pair<int, string> elem : mapStudent) {
    cout << elem.first << " " << elem.second << endl;
  }
}



references:
http://www.kuqin.com/cpluspluslib/20071231/3265.html
http://en.cppreference.com/w/cpp/language/range-for

The Beginner’s Guide to Shell Scripting: The Basics 1

there are a few guidelines you need to know.


  1. Every script should being with “#!/bin/bash”
  2. Every new line is a new command
  3. Comment lines start with a #
  4. Commands are surrounded by ()