Showing posts with label script tricks. Show all posts
Showing posts with label script tricks. Show all posts

usage tips for Linux

# to search for a key word in files with certain extension
find . -iname "*.cc" -exec grep -Hin "thekeyword" {} \;
# -i to ignore case
# -H will print out the file name which matches to the filter
# -n will print out the matching line number

#if you want to use some filter containing escape char, use single quote
find . -name "*.cc" -exec grep 'setData(\"Pass'  {}\;

#to count the number of occurrence of certain word/pattern in vim
:%s/pattern//gn

# to count number of files in a directory
find . -type f | wc -l

# to change command prompt to { $user_name+@+$hostname +":" + $cwd> }
set prompt="\n%{\033[0;32m%}%n@%m:%{\033[0;33m%}%~%{\033[1;30m%}>%{\033\[1;37m%} "

# to use backspace to delete
stty erase ^H

# to pipe error and cout to file; for tcsh and csh only
./genKB_DA.sh >& ./log.txt

# about FTP downloading
use 'hash' to show download progress
use 'prompt' to turn off the 'y/n' prompt which blocks mget

# to check linux kernel version
uname -r

# to check red-hat version
cat /etc/redhat-release

# log in first as 'test' user, and then type the following to allow any user to access current x11 display
xhost +localhost

# to batch delete files based on strings in a file
xargs rm < ../fxxTN1.txt
How to schedule Tortoise SVN update as a win7 task?

* Since Tortoise SVN 1.7, during installation, there is an option to install command line svn utility, tick it, and you will find "svn.exe" installed in TortoiseSVN installation directory (e.g. C:\Program Files\TortoiseSVN\bin). By default, there is no svn executable installed.

* Create a batch file if you want to batch svn up for a few repository checkout.


  • REM To go to the script directory
  • cd /d %0\..

  • CD "a b c"
  • svn up
  • CD ../D_trunk
  • svn up

  • @ECHO OFF
  • echo "Good morning, svn update is done ..."



* from win7 startup, run "taskschd.msc", add the batch file as a scheduled task. It is quite intuitive.




on makefile

some notes on makefile:

The basic basic rule: 'target' depends on 'prerequisites' and the relationship is defined by 'command'.
target ... : prerequisites ...
command

version 1
objects = main.o kbd.o command.o display.o \
insert.o search.o files.o utils.o
edit : $(objects)
cc -o edit $(objects)
main.o : defs.h
kbd.o : defs.h command.h
command.o : defs.h command.h
display.o : defs.h buffer.h
insert.o : defs.h buffer.h
search.o : defs.h buffer.h
files.o : defs.h buffer.h command.h
utils.o : defs.h
.PHONY : clean
clean :
rm edit $(objects)

version 2
objects = main.o kbd.o command.o display.o \
insert.o search.o files.o utils.o
edit : $(objects)
cc -o edit $(objects)
$(objects) : defs.h
kbd.o command.o files.o : command.h
display.o insert.o search.o files.o : buffer.h
.PHONY : clean
clean :
rm edit $(objects)

.PHONY is followed by a pseudo target and it only defines a series of action, rather than files dependencies.

.PHONY : clean
clean :
-rm edit $(objects)

The '-' in front of rm means 'go ahead' about the action and do not stop for errors.

A few prime tools on unix programming

My previous experience with unix has limited to cc/gcc, vim, shell scripts, .cshrc and knowledge of some configuration files.


Recently, due to my job needs, I have tried to learn some make/nmake programming. After 1,2 days googling, I managed to dig up two good references. One is the 'NMAKE reference' from MSDN, and it is dedicated to Microsoft NMAKE, of course. I didn't find any downloadable version. The other is a tuotorial-like blog series on GNU MAKE usage from a very experienced Chinese programmer (google 跟我一起写Makefile). Fans have spent time to put the series together into a more readable PDF file. It is long, complete with many useful examples. It is very useful as both tutorial and reference manual.

Here, ladies and gentlemen, I am going to introduce some byproducts from my learning journey. (Thanks to the 2nd reference mentioned above.) You may already heard the name of SED and AWK, two famous unix utilities. What are they? read the following from: http://www.faqs.org/docs/abs/HTML/sedawk.html

sed: a non-interactive text file editor
awk: a field-oriented pattern processing language with a C-like syntax


Despite all their differences, the two utilities share a similar invocation syntax, both use regular expressions, both read input by default from stdin, and both output to stdout. These are well-behaved UNIX tools, and they work together well. The output from one can be piped into the other, and their combined capabilities give shell scripts some of the power of Perl.


so, try it!

Some very very useful unix command !!!

There is something I wish I could remember and forget again and again, so I note them down.

.tar ; use tar -xvvf to un-tar them
.gz ; use uncompress

find . -name xxx ; search for a file or dir named xxx in the current directory

rm ; to delete a file
rm -r ; to delete a directory

more ; to see the first few lines of a file
less ; to see the last few lines of a file

rpm -ivh packageName ; to install a rpm package

! you do want to miss this -- ultra ugly tricks in batch

here I will introduce three ugly tricks to do one simple task -- get the batch file path from inside

1. very ugly, appears very sophisticated, very profound

for /f %%i in ("%0") do set curpath=%%~dpi
cd /d %curpath%

[%0 returns the file path and name that your typed at command prompt to run the batch, and ~dp means to get drive and path]

2. much more simpler, and easier to understand

cd /d %~dp0

[see explanation in item 1]

3. simple and maybe can not be simpler

cd /d %0\..

[do I need to explain? ]

some useful windows batch file command

@ECHO OFF:: Check Windows version -- Windows 2000 or later

IF NOT "%OS%"=="Windows_NT" GOTO Syntax
VER FIND.EXE "Windows NT" >NUL
IF NOT ERRORLEVEL 1 GOTO Syntax

:: Save a list of ALL shares found on the source server
NET.EXE SHARE > "%~dp0%ComputerName%_originalshares.txt" 2>&1