Friday, April 17, 2015

Cool Shell Utilities

In this page, I will try to include the cool bash shell utilities that will help us a lot.


Task 1. Search all files in sub directories in iterative way

find . -type f -name '*.mp3'  | while read i
do 
      echo $i
done


Alternative:

for file in 'find . -iname "*.mp3" 
do 
    echo $file
done 

Task 2. Create python/scala directly runnable from script

For Linux system, bash is very powerful. We can do almost anything using shell scripts. And Ubuntu comes with python already installed. So, to write python scripts is very straightforward. And if you have already installed JVM, then a more powerful and faster programming language "Scala" makes it even better to write scripts.  So, basically, I wanted to write, a python or scala program and run from a shell script.

Python:
 //1.py
#!/usr/bin/env python
print("hellow")

# mroe python commands






Run: ./1.py


Scala:
//1.scala
#!/usr/bin/env scala
println("hellow")


Run: ./1.scala

or 

//1.sh
#!/bin/sh
exec scala $0 $@
!#
// Say hello to the first argument
println("Hello, " + args(0) + "!")


This one is interesting!

It can be exectued as normal script

sh  1.sh 
or 
./1.sh

File extensions do not matter after we define environment