Shell Cheatsheet

Assigning Variables

#!/bin/sh
length=80
chapter=chap # no spaces
title="Greetings from Space" # with spaces
empty= # empty variable
size=$length

Displaying Variables

#!/bin/sh
echo $length # 80
echo length=$length # length=80
echo this is $title # this is Greetings from Space
echo ${chapter}5 # chap5 - to display with no space between variable and other parts of string

Special Variables

#!/bin/sh
$? # exit status of the last command
$$ # process number of the current shell
$! # process number of the last background command
$0 # filename of the current script
$# # number of arguments
$n # arguments (n = 1, 2, ...)
$@ # equivalent of $1 $2 ... $n
$* # same as $@

Special Symbols and Escaping

  • Double quotes escape special characters, except for $, `, \c (c = any character)
  • Single quotes escapes $ as well
  • Backslash escapes single character
  • Combinations with backsllash:
    • \n – new line
    • \c – stay on the same line
    • \t – tab

#!/bin/sh
echo * # equivalent to ls (print out current dir)
echo "*" # prints *
echo "$length" # prints 80 - $ is not escaped by double quotes
echo '$length' # prints $length
echo \$length # prints $length - escapes single character

Reverse quotes (`command`) – run command:

#!/bin/sh
echo today is `date` # shows today's date
users=`who | wc -l`
echo $users # displays number of users logged in

Arguments Shift

#!/bin/sh
# script called like this:
# thisscript hello world
a=$1; shift
b=$1
echo $b $a # world hello

Operators

&& - second command will only run if the first one was successful
|| - second command runs only if first one fails

#!/bin/sh
test -d ~/b && echo "b is directory" # will show "b is directory" only if b is directory
test -f ~/b || echo "b is not file, therefore b is directory" # equivalent to above

For and list

#!/bin/sh
for i in here there everywhere # list
do
   echo $i
done
# will print 
# > here
# > there
# > everywhere

Lists are separated by ; or new line

If-else

#!/bin/sh
if [condition1]
then
    echo "condition 1"
else
    echo "condition 2"
fi

# or
if [condition1]; then
    echo "condition 1"
else
    echo "condition 2"
fi
Conditions
  • -a - and
  • -o - or
  • -gt - greater then
  • -lt - less then
  • -eq - equal
  • -ne - not equal
  • -ge - greater or equal
  • -le - less or equal

#!/bin/sh
if [ $i -gt 7 -a $i -le 99] #$i between 7 and 99

While and Until

#!/bin/sh
# While
while [condition]; do
  [command]
done

# Until
until [condition]; do
  [command]
done

Case

#!/bin/sh
case $i
in
  1) continue;;
  2) echo $i
     continue;;
  3) break;;
esac

Arithmetic Operations (expr)

#!/bin/sh
n=0
n=$n+1
echo $n # prints 0+1 - treats as string by default

n=0
n=`expr $n+1`
echo $n # prints 1

n=`expr 2+3`
echo $n # prints 5
expr 5 "*" 6 # 30
Advertisements
Shell Cheatsheet

How to calculate and print number of files in each subfolder of the current folder on Linux

find . -type d | while read DIR; do find $DIR -type f | printf "%-8s %s\n" $(wc -w) $DIR; done | sort -nr

That is: find all subdirectories of current directory; for each subdirectory, find all files, and print file count and path. Finally it sorts the result to show folders with most files on top
Note that the first entry is the folder itself. Also this script omits soft links and does not count sub-folders as files.

For example:

[someone@somewhere]# cd /etc/X11/
[someone@somewhere]# find . -type d | while read DIR; do find $DIR -type f | printf "%-8s %s\n" $(wc -w) $DIR; done | sort -nr
10 .
7 ./xinit
3 ./xinit/xinitrc.d
0 ./xorg.conf.d
0 ./xinit/Xclients.d
0 ./fontpath.d
0 ./applnk

How to calculate and print number of files in each subfolder of the current folder on Linux

Infographic Tools

Tableau Public
Requires tool download (Windows only) and creation of the account, and allows to publish charts online, as well as store them as PDF or image. Very powerful set of graphs, and ability to create a dashboard. Allows to load data set from many different sources.

Infogr.am
Requires to create an account (Facebook / Twitter login can be used). Good selection of charts (data can be imported from Excel spreadsheets), which can also be wrapped into a few themes (and can contain graphical text, images and charts). Simple intuitive UI. No graphs or trees, however, and no option to export (can only publish online).

Easel.ly
Requires to create an account. You can choose a theme from many different templates, or create a new one from scratch.  Shapes are a central piece of the design, and can create many different forms: from tree and graph to a diagram. Presentations can also contain images and graphic text, but no ability to create charts based on data. Can be published online or exported as JPG.

Many Eyes
IBM site, based on Cognos, that allows to visualize data sets. Site design, and user experience in general are quite outdated, clumsy and slow (Java applets are used to render charts). However has a decent set of chart types, diagrams, and maps. Result can be published online or exported as PNG.

Charts Bin
Requires to create an account. Allows to upload and visualize location-based data. Has a few options, and user interface is not very smooth. However what’s valuable is the ability to create interactive maps with data, allows to publish or download it as PNG.

Infographic Tools

How do you like your Eclipse cooked?

Here’s how I like mine at the moment:

Platform

At home: Eclipse IDE for Java Developers – almost bare bones IDE, with only CVS, EMF, GEF, JDT, Mylyn, XML Tools (see: Eclipse packages comparison).

At work: SpringSource Tool Suite with Google integration – includes all of the above plus Spring and Google App Engine integration.

I also add Java VM path parameter to eclipse.ini file, so that I know exactly which java will be selected by Eclipse, i.e.:

-vm
C:/Program Files/Java/jdk1.6.0_26/bin/javaw.exe

Languages & Project support

Planning & Design

Debugging & Testing

to be updated periodically…

How do you like your Eclipse cooked?

PowerShell Syntax HighLighting with PowerShell Script

For script in the previous post, I needed a tool that would convert PowerShell script to HTML format. Surprisingly numerous online tools somehow completely ignored PowerShell, so I found PowerShell scripts that would do the job: PowerShell Syntax Highlighting by Lee Holmes, an extended version by Helge Klein, and different implementation, which will only work on PowerShell 2.0 by Jeff Hillman.

PowerShell Syntax HighLighting with PowerShell Script

Post Issue to Jira Using PowerShell 2.0

Some time ago I was looking for a skeleton of a PowerShell 2.0 script that would allow me to post an issue to Jira. Didn’t find one, so had to come up with my own.

# Change those strings to whatever is specific to environment
[string] $SERVER = “localhost:8080”
[string] $USER = “powershelluser”
[string] $PWD = “password”
# Get Project ID from Administration -> Projects, e.g. 
[string] $PROJECTID = “10123”
# Specify fields that need to be posted
# NOTE: You must provide all required fields
# Example below submits 4 fields: 
#    assignee – automatically assigned to default user of the project
#    issuetype – selected type 3 = task
#    summary and description, which are read from elsewhere
[string] $FIELDS = “issuetype=3&priority=3&assignee=-1&summary={0}&description={1}”

# Those constants normally do not change
[string] $LOGIN_URL = http://{0}/login.jsp?os_username={1}&os_password={2}&os_destination=/secure/”
[string] $CREATE_URL = http://{0}/secure/CreateIssueDetails.jspa?pid={1}&{2}&Create=Create&atl_token={3}”
[string] $TOKEN_PREFIX = ‘<meta id=”atlassian-token” name=”atlassian-token” content=”‘
[string] $TOKEN_SUFFIX = ‘”>’

function loginToJira($server, $user, $pwd) {

[string] $postUrl = $url -f  $user, $pwd

[System.Net.WebRequest] $webRequest = [System.Net.WebRequest]::Create($postUrl)
$webRequest.CookieContainer = New-object System.Net.CookieContainer
[System.Net.WebResponse] $webResponse = $webRequest.getResponse()
# TO DO: Response can be verified here
# One of the ways to check if request was successful is to read $webResponse.StatusDescription, e.g.
Write-Host “status = “ $webResponse.StatusDescription;

$resStream = $webResponse.GetResponseStream()
[System.IO.StreamReader] $streamReader = New-Object System.IO.StreamReader -argumentList $resStream
$res = $streamReader.ReadToEnd()
$start = $res.IndexOf($TOKEN_PREFIX, 0+ $TOKEN_PREFIX.Length
$end = $res.IndexOf($TOKEN_SUFFIX, $start)
$token = $res.substring($start, $end  $start)

return $webRequest.CookieContainer, $token
}

function createJiraIssue($server, $session, $token, $project_id, $summary, $description) {

[string] $formattedFields = $FIELDS -f [System.Web.HttpUtility]::UrlEncode($summary), [System.Web.HttpUtility]::UrlEncode($description)
[string] $postUrl = $CREATE_URL -f $server, $project_id, $formattedFields, $token

[System.Net.WebRequest]$webRequest = [System.Net.WebRequest]::Create($postUrl);
$webRequest.CookieContainer = $session
[System.Net.WebResponse]$webResponse = $webRequest.getResponse()
# TO DO: Response can be verified here (similar to login)

$resStream = $webResponse.GetResponseStream()
[System.IO.StreamReader]$streamReader = New-Object System.IO.StreamReader -argumentList $resStream
$res = $streamReader.ReadToEnd()
# TO DO: Here it can be verified if the issue was posted successully, by looking into $res
# If issue is not posted, Jira will return HTML similar to the page seeing by the user who tried to post issue with missing / problematic fields
}

if($args.count -ne 3) {
showHelp
break
}
Add-Type -AssemblyName System.Web
$session, $token = loginToJira $SERVER $USER $PWD
$testsummary = “This is a test – issue summary”
$testdscription = “This is a test.`n`nIssue description usually contains several lines of text”
createJiraIssue $session $token $PROJECTID $testsummary $testdscription

Post Issue to Jira Using PowerShell 2.0

Restricting Excel Cells to a List and Showing Drop-Down

If the list of values is located on the same sheet:

  1. Select cells you want to attach list to
  2. Select Data > Data Validation
  3. In the Data Validation dialog, Settings tab, set:
    Allow: List
    Source: [range, that represents list of values in the list, e.g. “=A1:A5“]
  4. Click OK

In many cases, however, it’s more convenient to store list contents on separate sheet. But if you try to specify Source from a different spreadsheet (e.g. “=Sheet2!A1:A5“), Excel will throw an error:  “The formula you typed contains an error. <…>“. Apparently Excel does not allow range from a different spreadsheet. But there’s a nice workaround, which I digged out of internet long time ago, and can’t remember the source. What you need to do, is to create a named range, and use it instead of the cell range.

Creating a named range:

  1. Go to a sheet where list is located, and select all list items.
  2. Right click on selected cells and choose Name a Range… or select Formulas > Define Name
  3. Specify Name for the range (e.g. ‘MyList‘) and verify that Refers to range is correct.
  4. Click OK.

Now you can use a named range you just created in the Data Validation:

  1. Select cells you want to attach list to
  2. Select Data > Data Validation
  3. In the Data Validation dialog, Settings tab, set:
    Allow: List
    Source
    : [equal sign and the name of the range you just created “=MyList“]
  4. Click OK
Restricting Excel Cells to a List and Showing Drop-Down