Live Chat!

download multiple files from a site using for loop like c in bash

* * * *   4 votos

February 7th, 2007 mysurface

Batch download using wget seems to be not working anymore, a lots of web site used to protect the particular directory from being browse. But if you know the filename of the files, you still can do a batch download by writing a custom bash script.
Recently I discover a site which allows you to [...]

Posted in Bash, for | Hits: 15499 | 6 Comments »

Print color text in command line.

* * * *   1 votos

December 24th, 2006 mysurface

Sometimes echo is not enough, if you need to print more advanced format of text. Lucky, we have printf. printf is a common function call in c programming language, if you learn c before, you should very familiar with this function.
Simply illustrate the power of printf, do as follow:
printf ‘\n\t\thello\tworld\n’
Besides readable character, printf can accept [...]

Posted in Text Manipulation, clear, printf, read | Hits: 25055 | 7 Comments »

Rename multiple files

* * * * ½ 7 votos

November 5th, 2006 toydi

Often over time, we will want to reorganize a group of files by renaming them.
To rename *.txt to *.bak
(e.g. to rename ham.txt to ham.bak)
for f in *.txt; do mv “$f” “${f%.txt}.bak”; done
To remove ‘new-’ from new-*
(e.g. to rename new-ham.txt to ham.txt)
for f in new-*; do mv “$f” “${f#new-}”; done
${variable%pattern} vs ${variable#pattern}
The funny-looking symbol, ${f%.txt} [...]

Posted in Common, Regular Expression, for, mv | Hits: 74902 | 14 Comments »

Generating Random numbers

* * * * * 1 votos

October 27th, 2006 mysurface

We can obtain pure random (NOT pseudo-random) bytes from /dev/random. Linux kernel harnesses a good source of randomness from you. The random bytes in /dev/random is measuring based on the time delay between your input actions.
so seems /dev/random provided random bytes, we need to turn the bytes into an integer so that we could read. [...]

Posted in Bash, Misc, echo, od, touch | Hits: 9418 | No Comments »

get help while writting shell scripts

* * *     1 votos

October 22nd, 2006 mysurface

sometimes I just forgot the syntax of using if, while or for. Sometimes I am not sure the syntax i used is it correct. Let say I wanna refers back how to use square bracket ( [ ] ), by specified
man “\["
It leads me to a lengthy BASH_BINUTILS manual, which cause me more headache. [...]

Posted in Bash, Misc, help | Hits: 4621 | No Comments »

read line by line

* * * * ½ 3 votos

October 18th, 2006 mysurface

When writting shell script, usually people are looking for how to read line by line and manipulate the lines, the simple way of using “read” shows as bellow
while read line
do
echo “__ $line”
done < file.txt
The example above simple add 2 underscore and a space infront of each line.
By default read is line by line, [...]

Posted in Bash, read | Hits: 8746 | 1 Comment »

pass variable to awk from outside

* * * * * 2 votos

October 17th, 2006 mysurface

Many people uses awk in their shell script, but they need to pass variable to awk for manipulation, it is not difficult, bellow is a simple example.
awk ‘END { print myval }’ myval=`pwd` data.txt
This example is to show you, I successfully pass a value from outside, at the END of the manipulation, print me my [...]

Posted in Bash, Text Manipulation, awk | Hits: 8019 | 1 Comment »

if … then … fi

* * * * * 2 votos

October 13th, 2006 mysurface

if iis a basic programming checking for condition and reacts based on the condition, bash uses if to check for

File
integer
String

For checking the File, let say your file name is stored at $fn, and you wanna check the file is exist ? do this:

if [ -e $fn ]
then
echo “$fn exist.”
else
echo “$fn does not exist.”
fi
Bellow are [...]

Posted in Bash, Misc, echo, if | Hits: 9357 | No Comments »

writting executable script

          0 votos

October 8th, 2006 mysurface

Shell script (sh), Bash script (bash), Python and perl are all scripting language. By default, to run a scripting file, for example Python script, you need to do this:
python myscript.py
Another example for shell script:
sh myscript.sh
But we make is executable and can execute by running directly like this
./myscript.py
If you place your script into directory that exported [...]

Posted in Bash, Developer, chmod, file, python, sh, which | Hits: 12514 | 1 Comment »

Run multiple commands consequently

          0 votos

October 3rd, 2006 mysurface

By specified terminator ( ; ) like in C/C++ programming, you can run multiple commands consequently. We usually want to do this when we want to compile and install source code for certain packages.
./configure;make;make install
Bash first execute the configure, then make , consequently will install the package. But bare in mind, bash have no intelligent, [...]

Posted in Symbol, du, read, terminator, while | Hits: 14465 | 3 Comments »