extracting substring at bash

June 28th, 2007 mysurface

Extracting sub string at Bash is very easy, let say you have a phone number 012-4567890 and you just wanna extract 4567890 out, you can do as below. num=”016-4567890″; echo ${num:5:7} 5 is the starting point and 7 is the string length for sub string. Related Poststar normal operationI am here to show you examples […]

Posted in Bash, Text Manipulation | Hits: 143832 | 15 Comments »

CLI Magic: command not found, suggest what to apt-get

May 5th, 2007 mysurface

I have recently upgrade my Ubuntu Edgy to Ubuntu Feisty and I discover a fantastic CLI magic packaged with Feisty, its call command-not-found. With command-not-found package, when you have type some command that doesn’t exist in your terminal, it will suggest you what to install. Let say I type trafshow, which It is not installed, […]

Posted in apt-get, Bash, command-not-found, which | Hits: 75616 | 7 Comments »

CLI magic: programable bash completion

May 4th, 2007 mysurface

Ubuntu, a Linux distribution that is getting popular because it has successfully improves it’s user friendliness with simple User interface. You can install any applications with few clicks, configuration under GUI dialogs etc. People usually have the thinking of maybe one day, Ubuntu will become another new operating system like Microsoft Windows. Never! The concept […]

Posted in apt-cache, apt-get, Bash, complete, pipeline, sort, svn | Hits: 129420 | 11 Comments »

Bash: timed read input

March 13th, 2007 mysurface

For some critical task, you just can’t wait for user response forever. If the user do not respond to input after certain period of time, you will use the default value and carry on the process. The bash command read have such capability to support that. A simple example to illustrate how it works answer=”yes”;read […]

Posted in Bash, read | Hits: 56687 | 2 Comments »

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

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: 66559 | 11 Comments »

Print color text in command line.

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, […]

Posted in clear, printf, read, Text Manipulation | Hits: 224306 | 19 Comments »

Rename multiple files

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 […]

Posted in Common, for, mv, Regular Expression | Hits: 216372 | 29 Comments »

Generating Random numbers

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 […]

Posted in Bash, echo, Misc, od, touch | Hits: 112982 | 10 Comments »

get help while writting shell scripts

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 […]

Posted in Bash, help, Misc | Hits: 31072 | 1 Comment »

read line by line

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 […]

Posted in Bash, read | Hits: 59576 | 3 Comments »