Programmer's Blog

Programmer's reference

Monthly Archives: May 2014

[Linux] Add user and Groups

To add a user and change its password

$> useradd -d /home/john john
$> passwd john

To create a group

$> groupadd developers

To add a user to group(s), the first 3 groups are in order with first group as default

$> useradd -G emp1,mgt,developers john
$> usermod -G -a newgroup john   //add group
$> usermod -g emp1 john  //change primary group

To check the possession of group

$> id john
output:
$> uid=23(john) gid=283(emp1) groups=242(mgt) 384(developers) 723(newgroup)

Then assign authority to a folder

$> chown -R john:emp1 /opt/mylib

[NuoDB] Create Primary key with sequence

Create table <table_name> (id INTEGER PRIMARY KEY GENERATED ALWAYS AS IDENTITY (tst_seq));

[Oracle] Show number of record in each table

Simply as

SQL> select table_name, num_rows from user_tables;

[Shell] Find bin path using env

In the previous example of using expect the bin file is specified using the following syntax

#!/usr/bin/expect

If the path to expect is not found, try

#!/usr/bin/env expect

This will cause the execution to search for the excutable

[Oracle DB] Query plan

The following procedure helps to show the query plan of the SQL

SQL> EXPLAIN PLAN FOR SELECT * FROM TABLE;

Explained.

SQL> SELECT PLAN_TABLE_OUTPUT FROM TABLE(DBMS_XPLAN.DISPLAY());

OR

SQL> SET AUTOTRACE ON EXPLAIN;
SQL> SELECT * FROM TABLE;

to close autotrace
SQL> SET AUTOTRACE OFF;

SAMPLE OUTPUT:
Plan hash value: 1046824173

---------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
---------------------------------------------------------------
| 0 | SELECT STATEMENT | | 16 | 1440 | 7 (0)| 00:00:01 |
| 1 | TABLE ACCESS FULL| TABLE | 16 | 1440 | 7 (0)| 00:00:01 |
---------------------------------------------------------------

[SSH] Port forwarding

suppose we have host1 (local) and host2, we want to forward host2:10001 to host1:10000

so that we can access the host2 port 10001 by opening it on port 10000 of host1.

someone@host1$> ssh -f -g -N -L 10000:localhost:10001 someone@host2

To remove the port forwarded, use  the following to find and kill the ssh process

someone@host1$> ps aux | grep ssh

[NuoDB] Find the next value of the sequence

select next value for <sequence name> from dual;

[C++] Parse delimited string into array

-----parsing numbers-----
#include<vector>
#include<string>
#include<sstream>

......
std::string str ="1,2,3,4,5,6";
std::vector<int> vect;

std::stringstream ss(str);
int i;
while(ss >> i)  {
    vect.push_back(i);
    if(ss.peek()==',')
        ss.ignore();

}

-----parsing strings------
std::string s = "scott>=tiger>=mushroom";
std::string delimiter = ">=";

size_t pos = 0;
std::string token;
while ((pos = s.find(delimiter)) != std::string::npos) {
    token = s.substr(0, pos);
    std::cout << token << std::endl;
    s.erase(0, pos + delimiter.length());
}
std::cout << s << std::endl;

Execute statements in each second

This is a C++ example to perform execution in each clock second, the method is recording the start time, use a double while loop to catch the difference “tick = diff(end, start)” between end time and start time.

#include <time.h>

time_t start, end;
double tick;

while(1) {
    time(&start);      //catch start time

      while(1) {
             .....(must sleep <1 second, otherwise CPU surges)
             //boost::this_thread::sleep(boost::posix_time::microseconds(5));
             time(&end);
             tick = difftime(end, start);

             if (tick == 1) {
                    ... actions performed in each second
             }
      }
}

[Java] Set thread priority

Create Thread object and use setPriority method to rise the value of NORM_PRIORITY parameter

Thread thrd1 = new Thread("High");
Thread thrd2 = new Thread("Low");

thrd1.setPriority(Thread.NORM_PRIORITY+2);
thrd2.setPriority(Thread.NORM_PRIORITY-2);