Posts tagged ‘bash’

Short how-to:

root@215003:~# mysql -u root -p
Enter password:
mysql> CREATE DATABASE s_db;
mysql> GRANT ALL PRIVILEGES ON s_db.* to 's_user'@'localhost' IDENTIFIED BY '12345678';
mysql> FLUSH PRIVILEGES;
mysql>exit
mysql -u s_user -p s_db < database.sql
<
code>

Here it is ( with user name, password, database host and db name ) :
mysql -uuser_name -ppassword -hmysql_host_server_.domain.com -Ddatabase_name < database_commands.sql

You can use sed :

sed -e 's/\([^\d]*\)/\L\1/' in.txt > out.txt

or perl inliner :

perl -ne 'utf8::decode $_; $_ = lc $_; utf8::encode $_; print' in.txt > out.txt
.
Both guys works fine for unicode file too.

Using telnet in bash scripts to automate some stuff ? It’s really easy, here’s an example to tell “hello world” on telnet server :

req="hello world"
server="my_server 1234"
val=`( echo open ${server}
sleep 3
echo "${req}"
sleep 1 ) | telnet`
echo $val

Okay, we have text file with list of urls and want to have firefox’s screenshots from this pages and also we need to have this screenshots in some normalized resolution ( like all images should be in 300×400 – thumbnails ). First of all you need to install Command line print Firefox add-on. Then create some simple script which will run firefox with needed url, print screenshot and close ( in my case via kill – may be it’s too brutal ) firefox in cycle. It may look like this ( url_list.txt – file with urls – each url on its own line :-)), after running this script you will have many *.png files which is screenshots for ulrs – 0.png – for first url in urls_list.txt, 1.png for second and so on.

#!/bin/bash
id=0
while read line
do
firefox -print $line -printmode png -printdelay 10 -printfile ${id}.png
ps ax | grep firefox | awk '{ print $1 }' | xargs kill -9 ;
id=$[$id+1]
done < urls_list.txt

And now then we have screenshots ( all this guys are in different resolution in common ) then we need to normalize them – to create thumbnails for all images in 300×400 resolution – convert helps!

for f in *.png;
do
convert -thumbnail 300x400! ${f} thumb_${f}
done

And we have many thumb_*.pn with 300×400 resolution all. A little note – using resolution without ! sign will work in another way – resize will be processed proportionally with using resize only for one dimension ( bigger one ).

Using json in bash :

  • jsawk : work with an array of JSON objects read from stdin, filter them using JavaScript to produce a results array that is printed to stdout. For example get’s json ( from Yahoo image search by ‘madonna’ query ) and add some info ( madonna’s real name ) to this
  • wget http://bokov.net/json_example.json
    cat json_example.json | ./jsawk "this.ResultSet.RealName=\"Louise Ciccone\"" > updated_info.json

  • you can use some bash/awk/sed stuff to parse json. For example – list all image urls from json
  • wget http://bokov.net/json_example.json
    cat json_example.json | sed -e 's/[{}]/''/g' | awk -v k="text" '{n=split($0,a,","); for (i=1; i<=n; i++) print a[i]}' | grep '"ClickUrl":' | sed 's/:/ /1' | awk -F" " '{ print $2 }'

  • or use library from json.org for language which you like 🙂

via v.kruchkov

  • Recursively add files and foldes into CVS
find . -type d -print | grep -v CVS | xargs -n1 cvs add
  • Delete “.svn” files from current folder
find . -type f  -exec echo \"{}\" \;  | grep ".svn" | xargs rm

Here’s some steps which I frequently use to configure Ubuntu to work as EC2 manager
Continue reading ‘Quick steps to configure Ubuntu for with EC2 API’ »

Sun Grid Engine’s top engineer Richard Hierlmeier wrote article ( and some bash scripts which implements it – btw why you not to put it them onto your cvs? ) about using SDM in compute cloud ( here’s EC2 as example, I suppose that GoGrid can be used also without too many changes ) – Using SDM Cloud Adapter to Manage Solaris Zones.

My way – run sqlplus, spool in using html markup and convert this html onto text by using sed replacements. It works fast enought, and better than parse resulted text by spool without markup. Bash script which impelement it is under below Continue reading ‘How to dump oracle table into tab separated text file ( csv )’ »

Actually newbies in Solaris ( and experienced users in Linux :-)) have troubles with some every-day routines which works in Solaris in a different way than in most Linux like Ubuntu. Below I try to list most ”popular” problems and questins about differences in Solaris and Linux and try to figure them out.

  • grep doesn’t have -r switch, so there’s no recursive looking throught directories, here’s alternatives for recursive grep on Solaris :
    grep 'somestring' `find . -name '*'`
    find . | xargs grep 'somestring'
  • tar doesn’t support -z option, so tar xfz my_archive.tar.gz will fail with error “tar: z: unknown function modifier“. To unpack tar.gz archive on Solaris you may use this one :
    gzip -dc my_archive.tar.gz | tar xvpf -
  • how to set environment variables in Solaris ( i set them in \~/.bashrc, but it doesn’t works )  : to make Solaris read and apply to user environment barsh_rc file try to create file ~/.bash_profile in your home, and put in it lines listed below :
    if [ -f ~/.bashrc ]; then
    . ~/.bashrc
    fi

To be continued…

Usefull links : Solaris Infrequently Asked and Obscure Questions