Category Archives: Shell

Makefile rules & recipes

Recipes for the rules defined in your Makefiles require specific indentation. Each line in a recipe (i.e. “tests” below) must start with a tab character.

You can run:

cat -n -E -T Makefile
option description
n Show line numbers
e equivalent to -vE
t equivalent to -vT
E, –show-ends display $ at end of each line
T, –show-tabs display TAB characters as ^I
v, –show-nonprinting use ^ and M- notation, except for LFD and TAB

Which produces something like:

include ../Makeconfig$
headers := time.h sys/time.h sys/timeb.h bits/time.h^I^I^I\$
^I   bits/types/clockid_t.h bits/types/clock_t.h^I^I^I\$
^I   bits/types/struct_itimerspec.h^I^I^I^I\$
^I   bits/types/struct_timespec.h bits/types/struct_timeval.h^I\$
^I   bits/types/struct_tm.h bits/types/timer_t.h^I^I^I\$
^I   bits/types/time_t.h$
routines := offtime asctime clock ctime ctime_r difftime \$
^I    gmtime localtime mktime time^I^I \$
^I    gettimeofday settimeofday adjtime tzset^I \$
^I    tzfile getitimer setitimer^I^I^I \$
^I    stime dysize timegm ftime^I^I^I \$
^I    getdate strptime strptime_l^I^I^I \$
^I    strftime wcsftime strftime_l wcsftime_l^I \$
^I    timespec_get$
aux :=^I    era alt_digit lc-time-cleanup$
tests := test_time clocktest tst-posixtz tst-strptime tst_wcsftime \$
^I   tst-getdate tst-mktime tst-mktime2 tst-ftime_l tst-strftime \$
^I   tst-mktime3 tst-strptime2 bug-asctime bug-asctime_r bug-mktime1 \$
^I   tst-strptime3 bug-getdate1 tst-strptime-whitespace tst-ftime \$
^I   tst-tzname$

Where ^I represents a tab character and $ represents a newline character. You can use this to check for valid tab and newline indentation in your recipes in case you run into this error: *** missing separator. Stop.
If you’re using vi, make sure to use :set noet to disable replacement of tabs with a tabwidth set number of spaces.

List listening ports and applicationli

netstat -ano | egrep 'Proto|LISTEN'

  • -a Show the state of all sockets, including passive sockets used by server processes
  • -n Show network addresses as numbers
  • -o Show process Id

netstat -ano | grep 8080

Show which application is using that port:

netstat -anb | grep -A 1 "8080"

  • -b Display executable listening on port
  • grep
  • -A option is to display the preceding line after greps returned result

SED command line utility – split string (& grep)

SED is a useful Unix command line tool I use for simple text replacement.

Here’s an example to see the directories included in your Windows PATH environment variable.  (I use MinGW for a Unix-like Shell in Windows)

echo $PATH

displays all directories on one line separated by semicolons.

With sed, we can list each directory on its own line.

echo $PATH | sed 's/:/\n/g'

First we echo $PATH, then pipe the output to the sed command to do our string replacement. The /g modifier will substitute ALL matches, not just the first.

Now each directory is listed on a separate line.

Additionally, if you are looking for a specific directory, for example you want to see which SVN installation is included in your PATH, you can use the grep command.

echo $PATH | sed -e 's/:/\n/g' | grep -i 'svn'

Once again we pipe the output from sed and use grep to show only directories that include SVN. The -i option is to ignore case (if your folder name includes the text “Svn”, “SVN”, etc.)

Adding unversioned files from Linux command line

Add all unversioned files from the command line using regular expressions.

svn status | grep '^?' | cut -c9- | sed -rn 's/^(.*)$/\"\1\"/p' | xargs svn add

svn status | grep '^?'
lists only un-versioned files.

cut -c9-
grabs only filename and path by cutting up to the 9th column removing the ? symbol and whitespace

sed -rn 's/^(.*)$/\"\1\"/p'
surrounds the filename with “double quotes” in case there are any spaces or your command prompt has an issue with the slashes being used in the path.

xargs svn add
executes svn add for each file.