1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Opening app with Shell Script

Last updated at Posted at 2013-12-07

Create a bin dir and create a file called openapp. Open it with vim to edit.

$ mkdir ~/bin && cd $_
$ touch openapp
$ vim openapp
# !/bin/bash

if [ $1 = "pd" ]; then
  echo Opening CA PD website
  python -m webbrowser -t "https://sites.google.com/a/canacad.ac.jp/teaching-and-learning-systems/"
elif [ $1 = "atlas" ]; then
  echo Opening Atlas 
  python -m webbrowser -t "http://canacad.rubiconatlas.org/Atlas/Authentication/View/Login"
elif [ $1 = "work" ]; then
  echo Opening Powerschool
  python -m webbrowser -t "https://cadata.canacad.ac.jp/teachers/pw.html"
  echo Opening Moodle
  python -m webbrowser -t "http://moodle.canacad.ac.jp/my/"
  echo Opening Gmail
  python -m webbrowser -t "https://mail.google.com/mail/ca/u/0/#inbox"
  echo Opening Google Drive
  python -m webbrowser -t "http://drive.google.com/a/canacad.ac.jp/?tab=wo#my-drive"
  echo Opening Google Calendar
  python -m webbrowser -t "https://www.google.com/calendar/render?tab=wc"
  echo Opening Youtube
  python -m webbrowser -t "http://www.youtube.com/playlist?list=FLeZl-6wion9rnFwOyOWcQNw&feature=mh_lolz"
elif [ $1 = "tokyo" ]; then
  echo Opening Skype
  open /Applications/Skype.app
  echo Opening Teamviewer
  open /Applications/TeamViewer\ 8/TeamViewer.app
elif [ $1 = "kobito" ]; then
  echo Opening Kobito
  open /Applications/Kobito.app
elif [ $1 = "word" ]; then
  echo Opening Word
  open /Applications/Microsoft\ Office\ 2011/Microsoft\ Word.app
fi                                

Add x to permission

$ chmod +x openapp

Create closeapp

# !/bin/bash

if [ $1 = "tokyo" ]; then
  echo Closing Skype
  killall Skype
  echo Closing Teamviewer
  killall TeamViewer
elif [ $1 = "word" ]; then
  echo Closing Word
  killall Microsoft\ Word
elif [ $1 = "kobito" ]; then
  echo Closing Kobito
  killall Kobito
first

Add x to permission

$ chmod +x closeopp

Using case statement

# !/bin/sh

case $1 in 
 		init)
			echo initialized
			;;
		status)
			echo the current status
			;;
		test)
			echo running the tests
			;;
esac

Comparison operators

Unix-Shell Basic Operators

Check if a file exist or not.

# !/bin/sh

if [   -f $HOME/.zshrc  ]
then
	echo "true"
else
	echo "false"
fi

Creating a vim mapping

In vim type the following. To use it hit ctrl+t to run it.

  • map ctrl-t
  • : create a command line command
  • ! run it in a regular terminal
  • ./ current dir
  • % current file
  • cr carrage return or enter
  • to go back vim, just hit a enter
:map <C-t> :!./%<cr>

String operators

[ $a = $b ] compare two operands are equal
[ $a != $b] compare two operands are not equal
[ -z $a ] if $a is size zero
Other operands
-n for non-zero string

File test operators

[ -d $file ] check it for directory
-e for file and directory
-r if it is readable
-w writable or not
-x executable or not

RElational operators

[ $a -eq $b ] if the value of two operands are equal or not
-ne not equal
-gt greater than
-lt less than
-ge greater than or equal to
-le less than or equal to

Boolean operators

[ $a -lt 20 -o $b -gt 100 ] -o means OR
-a is AND

Loop

for loop example 1

loop.sh

for letter in a b c d
# or 
# for letter in "a b" "c d"
do 
	echo $letter
done

for loop example 2

loop.sh
# !/bin/sh

letters="a b c d"

for letter in $letters
do 
	echo $letter
done

for loop example 3

for item in $HOME/*
do
	echo $item
done

for loop example 4


for item in $HOME/sandbox/*
do 
	if [ -f "$item" ]
	then
		cat "$item" | awk 'BEGIN { print "--------" } { print NR, $0 }'
 	fi
done

while loop example 1

num=0

while [ $num -lt 10 ]
do
	echo $num
	num=$((num + 1))
	# this is the same as
	# num=`expr $num + 1`
done

while loop example 2

num=1

while read line
do
	echo $num $line
	num=$((num + 1))
done


$ cat loops.sh | ./loops.sh

This will print content with number lines.

Writing a shellscript with a different language

For example using Javascript.

# !/usr/bin/env node

process.stdin.resume();
process.stdin.setEncoding('utf8');

porcess.stdin.on('data', function (chunk) {
	process.stdout.write(chunk.toUpperCase());
});


Write full path to node or other language. Use /usr/bin/env for UNIX system.

$ chmod +x upper
$ cat upper

$ cat upper |  upper

$ cat file.txt | upper | sort | uniq
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?