Create a linux shell to wrap files to the corresponding wrapper folder

I have list of around 200 .log files, and I am in need of creating a wrapper for each file.
Laborious buffaloes may have a flow like: Copy file name > Right click and create a folder > Move the file into it, repeat 200 times. Seems ez.
And this case, beside those human-beings, linux shell is also a kind of that buffalo.
Ok let’s go.

What I have:

  • 20161208_access.log
  • 20161209_access.log

What I about to have:

  • 20161208_access\20161208_access.log
  • 20161209_access\20161209_access.log

Step 1:

[ducfilan@localhost]$ cd logs
[ducfilan@localhost logs]$ vi wrapper_creator
# wrapper_creator file.
for full_filename in *log; do # loop through the log files.
 filename="${full_filename%.*}" # get the file name without extension.
 mkdir -p "$filename" # create the folder.
 mv "$full_filename" "$filename" # move the file into it.
done
echo "Done!"

Ok done, save it.

Step 2:

In order to execute it, we need to grant the execute permission.

[ducfilan@localhost]$ chmod +x wrapper_creator

Step 3:

And execute it.

[ducfilan@localhost]$ ./wrapper_creator
Done!

Ok I have what I want.

How to auto compile sass/scss files in Eclipse

First of all, sass/scss compilation needs Ruby to work with.

So, ok, we will install it.

But, we need to use system wide Ruby, ’cause I tried with rvm but there comes some problems:

/usr/bin/env: ruby_executable_hooks: No such file or directory

So if you use rvm, switch it back to:

rvm use system

Now go to:

https://www.ruby-lang.org/en/documentation/installation/

Then to make sure that Ruby is successfully installed:

ruby -v

It should have some information like:

[ducfilan@localhost]$ ruby -v
 ruby 2.1.8p440 (2015-12-16 revision 53160) [x86_64-linux]

Now come to sass:

gem install sass
# su in case of permission errors

Once again to make sure:

sass -v

Some information like this means we’ve successfully installed it:

[ducfilan@localhost]$ sass -v
Sass 3.4.23 (Selective Steve)

Ok, preparation is done. Open your project in Eclipse.

Right click on it > Properties > Builder > New > Choose ‘Program’ then Ok.

2017-01-25_140725

We need to fill out some fields:

  1. Location
    • Get the location of sass using:
      • which sass
  1. Working Directory
    • Relative path to your folder, with ${workspace_loc:/…} as root. Use Browse workspace to make it ez.
  2. Arguments
    • --update ${workspace_loc:/QTEC-Web/public/sass}:${workspace_loc:/QTEC-Web/public/css} --sourcemap=none --style compressed
    • With:
      • –update: Means compiling when your sass file is changed.
      • ${workspace_loc:/QTEC-Web/public/sass}: Your sass folder
      • ${workspace_loc:/QTEC-Web/public/css}: Your output folder
      • –sourcemap=none: No generating .map file
      • –style compressed: Output style

For example in my case:

2017-01-25_141341.png

After that, don’t close the configuration screen.

Switch to Build options tab and check only following files, uncheck all others if they are pre-checked.

  1. Allocate Console
  2. During auto builds

Now you’re done, edit an .scss file and you will see the message of writing .css file in the output screen. If not, you’re doing it wrong.

How to connect to localhost using Postgres Admin (pgadmin) in CentOS

First of all, a recall how to install Postgres and pgAdmin3 in CentOS (CentOS 6.8, Postgres 9.5 in my case):

Postgres:

[ducfilan@localhost]# su

[root@localhost]# wget https://download.postgresql.org/pub/repos/yum/9.5/redhat/rhel-6-x86_64/pgdg-centos95-9.5-3.noarch.rpm

[root@localhost]# rpm -ivh pgdg-centos95-9.5-3.noarch.rpm

[root@localhost]# yum install postgresql95-server postgresql95-devel postgresql95-contrib

[root@localhost]# service postgresql-9.5 initdb

[root@localhost]# service postgresql-9.5 start

[root@localhost]# chkconfig postgresql-9.5 on

And pgAdmin:

yum install pgadmin3_95

To start Postgres Admin:

[root@localhost]# pgadmin3

When you add a server to connect to localhost database, the error message appears:

postgresql The server doesn't accept the current user: The server report
Ident authentication failed
The server doesn't accept the current user: The server reports 

FATAL: Ident authentication failed for user "pgadmin" 
If this message appears, the pg_hba.conf entry found for your 
client / user / database combination is set to "ident" authentication.  
Some distributions, e.g. Debian, have this by default. To perform ident  
based authentication successfully, you need additional setup; see the  
PostgreSQL help for this. For a beginner, it might be more appropriate  
to use a different authentication method; MD5 encrypted passwords are  
a good choice, which can be configured by an entry in pg_hba.conf like  
this: 

host all all 192.168.0.0/24 md5 

This example grants MD5 encrypted password access to all databases to  
all users on the private network 192.168.0.0/24. 
You can use the pg_hba.conf editor that is built into pgAdmin III to  
edit the pg_hba.conf configuration file. After changing pg_hba.conf,  
you need to trigger a server configuration reload using pg_ctl or by  
stopping and restarting the server process. 

Look at the message, it means that ident authentication is not usable here.
We have to edit the pg_hba.conf file. After several researches, I knew the location of pg_hba.conf file, open it up:

[root@localhost]# nano /var/lib/pgsql/9.5/data/pg_hba.conf

You will find some ident here, change it to md5 (username – password authentication)
But…
Dont forget to restart the postgres service:

<[root@localhost]# service postgresql-9.5 restart

Try again, the sky will be brighter.

What needs to consider when sorting arrays in Perl

I ‘ve working with a coding challenge, my solution leads me to sort an array of numbers.

As the Perl variables are scalar, and sorting syntax in Perl is as easy as eating a cookie.

Just:

@sorted = sort @unsorted;

But it’s not promissed the array elements are numbers, so by default, it will be sorted by alphabet order.

This will lead to: 11 < 2

So the result is not what we desire.

If we cannot debug the right case, it will be so frustrated.

Conclusion:

When sorting array of numbers, you have to pass a custom comparison function to Perl’s sort routine, as below:

@sorted = sort { $a <=> $b } @unsorted;