Thursday, October 9, 2014

Bash Params

Bash utils:

File tests

This section probably holds the most tests, I'll list them in some logical order. Since Bash 4.1, all tests related to permissions respect ACLs, if the underlying filesystem/OS supports them.
Operator syntaxDescription
-a True if exists. :!: (not recommended, may collide with -a for AND, see below)
-e True if exists.
-f True, if exists and is a regular file.
-d True, if exists and is a directory.
-c True, if exists and is a character special file.
-b True, if exists and is a block special file.
-p True, if exists and is a named pipe (FIFO).
-S True, if exists and is a socket file.
-L True, if exists and is a symbolic link.
-h True, if exists and is a symbolic link.
-g True, if exists and has sgid bit set.
-u True, if exists and has suid bit set.
-r True, if exists and is readable.
-w True, if exists and is writable.
-x True, if exists and is executable.
-s True, if exists and has size bigger than 0 (not empty).
-t True, if file descriptor is open and refers to a terminal.
-nt True, if is newer than (mtime). :!:
-ot True, if is older than (mtime). :!:
-ef True, if is a hardlink to . :!:

String tests

Operator syntaxDescription
-z True, if is empty.
-n True, if is not empty (this is the default operation).
= True, if the strings are equal.
!= True, if the strings are not equal.
< True if sorts before lexicographically (pure ASCII, not current locale!). Remember to escape! Use \<
> True if sorts after lexicographically (pure ASCII, not current locale!). Remember to escape! Use \>

Arithmetic tests

Operator syntaxDescription
-eq True, if the integers are equal.
-ne True, if the integers are NOT equal.
-le True, if the first integer is less than or equal second one.
-ge True, if the first integer is greater than or equal second one.
-lt True, if the first integer is less than second one.
-gt True, if the first integer is greater than second one.

Misc syntax

Operator syntax Description
-a True, if and are true (AND). Note that -a also may be used as a file test (see above)
-o True, if either or is true (OR).
! True, if is false (NOT).
( ) Group a test (for precedence). Attention: In normal shell-usage, the "(" and ")" must be escaped; use "\(" and "\)"!
-o True, if the shell option is set.
-v True if the variable has been set. Use var[n] for array elements.
-R True if the variable has been set and is a nameref variable (since 4.3-alpha)


Copied from here

Thursday, February 27, 2014

screen utility

Love using screen term its very very handy for development purpose, it has so many functions, even i'm not an expert in all of  thme.

Here are few things that are handy for daily usage:




Copy and Paste from screen buffer:
1) use  end of copy

2) To paste C + p ]


Search keyword in buffer
/SearchWord - Vi-like search forward.
?SearchWord - Vi-like search backward.
Still to come... 

Execute a command in remote PC

I want to patch a file in remote m/c and was trying to use ssh command, didn't know it was so easy ;)

Basics:
To execute a command in remote m/c
ssh user@ip

To execute multiple commands in remote m/c
ssh user@ip  'command | command'

To execute a local script in remote pc
ssh user@ip  'command' <  local_script

My command:
ssh user@ip 'patch -p0 --dry-run -d ' < patch_file.patch


More info from this link

Thursday, January 30, 2014

Power of Xargs

One can do many tedious tasks in linux easily using its commands, one such task can be:

Move all unwanted files in a code base to a directory.
a) either one can use 'svn st'   command and find all '?' files and manually copy and move them to other dir or

b) use power full linux utilities do it for you.
#svn st  | egrep -v 'M|X|A|cscope|tags|Performing'  | awk -F" " '{print $2}' | xargs -I {} mv {} not_needed/

i) 'svn st' list all files like modified, new etc.

In above command we carefully have to ignore display of modified files
'M' -modified
'A'  - newly added
'cscope' - cscope tags
'tags'  - ctags
'X'  - unknow type and any other unwanted types like file name starting with 'Performing' etc

ii) awk -F" " '{print $2}' --- display filenames


iii)  xargs -I {} mv {} not_needed/

[ {} is a placeholder meaning "the current argument". (You can use xxx or yyy or any other string instead of {} if you want, as well, and it'll do exactly the same thing.) -I implies -n1, because you want to act on each file individually.  ]
Courtesy - http://www.linuxplanet.com/linuxplanet/tutorials/6522/3

You are done, the above command moves all unwanted files to folder name not_needed in above case.

Caution: Above command may also move newly added file or any other svn extension that i've missed, add carefully same in egrep -v option.



magic with egrep

egrep with regex turns out to be very useful utility.

egrep can be used with following:
a) []   Sqaure brace also called Character Class
b) < >
c) {}
d) ()

a) Egrep with Sqaure brace:
One can use square brackets to identify the exact characters one is searching for.
    grep '[Cc]'  file_name      ->To search C or c in a file one can use


.... more to follow soon

Tuesday, January 28, 2014

Best articles on setting up tftp ftp on debain

Setup tftp
http://imranasghar.blogspot.com/2008/09/how-to-setup-tftp-server-on-debian.html

Setup ftp
http://www.server-world.info/en/note?os=Debian_7.0&p=ftp

Thursday, February 9, 2012

Quick search in Linux using egrep

Searching for a string in the code base may take time depending on its size.
As  it may have ".o, .a, .so, .bin etc.," extenstion files and   'grep/egrep' would search whole code base including list mentioned above.

Below procedure may come handy for quick search

a) Create a static file and include all file extensions you want to skip
b) Pass this file as egrep argument.
c) You can alias it to a command
d) tats it :)

The procedure is described here:
a)   [/home/sharad/bin]~> cat exclude-mygrep

.*.swp
*.svn-base
*.tar.gz
*.tar.bz2
*.tgz
*.tbz2
*.bz2
*.gz
*.jar
*.gif
*.png
*.jpg
*.pdf
*.zip
*.o
*.a
*.so
*.ko
*.img
*.bin
.*.*.cmd
*.mod.c
*.tmp
*.bak
\#\*\#
\.#\*
tags
cscope.out

b)  Now in .bashrc file, you can alias this like
alias  mygrep="egrep --exclude-from $HOME/bin/.exclude-mygrep -rn "

You can search for a string like:
 #mygrep  Sharad *