четверг, 2 октября 2014 г.

Mind the spaces

Often developers forget about possible spaces in file and directory names, which can lead to any kind of trouble from corrupted awk parsing to terrible consequences like removing important stuff. We'll consider the latter issue. E.g. when we find files we often need to perform some operations on them. So, this command
find . -name "*.log" | xargs rm -f
will remove all log files recursively...or maybe won't? The case is that 'find' produces output of one file (row) at a time and if the file path contains spaces it will be split by shell into several 'files' which 'rm' will try to delete. Consider the following project structure:
--> tree
.
├── test
├── testdir
│   └── remove.log
└── test dir2
    └── remove2.log

2 directories, 3 files
So lets remove logs:
--> find . -name "*.log" | xargs rm -f
and see what we have now:
--> tree
.
├── testdir
└── test dir2
    └── remove2.log

2 directories, 1 file
Oooops.... Where is the 'test' file? And why the 'remove2.log' is still here? Let's see what happens. I'll revert back to the original structure before the 'rm' and we'll run harmless 'ls' to see what xargs is getting:
--> find . -name "*.log"              
./testdir/remove.log
./test dir2/remove2.log
--> find . -name "*.log" | xargs ls -l      
ls: cannot access dir2/remove2.log: No such file or directory
-rw-rw-r-- 1 aikikode aikikode  9 Oct  2 16:17 ./test
-rw-rw-r-- 1 aikikode aikikode 13 Oct  2 16:18 ./testdir/remove.log
So shell is passing 3 files:

  • ./testdir/remove.log
  • ./test
  • dir2/remove2.log
instead of original two. And all because of spaces in the directory name. And it happens (actually it was intentional) so that we have a file 'test' with same name as 'test dir2' directory first name part if split by space. That's why we get wrong files as a result of 'find'. There're 2 options here:
  1. set IFS variable before executing the command:
    IFS="$(printf '\n\t')"
    you should be aware that syntax and usage of IFS might differ from shell you are using
  2. Use -exec option in find:
    --> tree
    .
    ├── test
    ├── testdir
    │   └── remove.log
    └── test dir2
        └── remove2.log
    
    2 directories, 3 files
    --> find . -name "*.log" -exec rm -f {} \;
    --> tree
    .
    ├── test
    ├── testdir
    └── test dir2
    
    2 directories, 1 file
    

вторник, 4 марта 2014 г.

Debian IPsec tunnel

Assume we have two servers in different subnets that we want to connect through IPsec.
# Alice
206.36.46.56
10.20.10.10  # local IP
# Bob
207.37.47.57
10.20.20.10  # local IP

понедельник, 24 февраля 2014 г.

Ubuntu - install Sun Java and browser plugin

Install Java

Since Oracle Java is no longer currently available in a supported Ubuntu repository, one should go to Java download page and download tgz archive. Currently the latest version is 7 update 51.
Assuming you downloaded it to ~/Download/jre-7u51-linux-x64.tar.gz:
sudo mkdir -p /usr/lib/jdk/
cd /usr/lib/jdk/
sudo tar xzf ~/Download/jre-7u51-linux-x64.tar.gz
sudo update-alternatives --install "/usr/bin/java"   "java"   "/usr/lib/jdk/jdk1.7.0_51/bin/java"   0
sudo update-alternatives --install "/usr/bin/javac"  "javac"  "/usr/lib/jdk/jdk1.7.0_51/bin/javac"  0
sudo update-alternatives --install "/usr/bin/javaws" "javaws" "/usr/lib/jdk/jdk1.7.0_51/bin/javaws" 0
You may also need to configure java alternatives (in case you have several java versions installed):
sudo update-alternatives --config java
sudo update-alternatives --config javac
sudo update-alternatives --config javaws

Install browser (chrome) plugin

See official page for up-to-date instructions or perform these commands:
  1. Become the root user by running the su command and then enter the super-user password.
    sudo -s
  2. Create a directory called plugins if you do not have it.
    mkdir -p /opt/google/chrome/plugins
  3. Go to Google chrome plugins directory before you make the symbolic link.
    cd /opt/google/chrome/plugins
  4. Create a symbolic link.
    ln -s /usr/lib/jdk/jdk1.7.0_51/jre/lib/amd64/libnpjp2.so .
  5. Restart your browser and test Java

Open jnlp files from webpages with javaws

Go to the webpage with jnlp application. Start the application and the browser will download the jnlp file. Click the arrow next to the file button in the downloads bar and select "Always open files of this type". Start the application again to see the effect.