Download presentation
Presentation is loading. Please wait.
Published byMelvin Adams Modified over 9 years ago
1
Shell Script Yingying Wang
2
Basic Commands
3
Good resources Google is your friend http://www.freeos.com/guides/lsst/
4
Example 1 $>nano hi.sh (or cat > hi.sh) #! /bin/bash echo “Hi Gaab Lab!” $>chmod u+x hi.sh $>./hi.sh (or sh hi.sh)
5
Example 2 $>cat > example2.sh if [ $# -ne 2 ] then echo "Usage - $0 x y" echo " Where x and y are two numbers for which I will print sum" exit 1 fi echo "Sum of $1 and $2 is `expr $1 + $2`“ Ctrl+D get out and return to command line $>chmod u+x example2.sh $>./example2.sh 1 2
6
Home Work Try to write a script to compute the sum for three numbers
7
Example 3 $>cat > example3.sh i=5 while test $i != 0 do echo "$i " i=`expr $i - 1` Done Ctrl+D get out and return to command line $>chmod u+x example3.sh $>./example3.sh
8
Example 4 #!/bin/bash echo "Hello, $LOGNAME" echo "Current date is `date`" echo "User is `who i am`" echo "Current direcotry `pwd`"
9
Example 5 #!/bin/bash if [ $# -ne 1 ] then echo "Usage - $0 file-name" exit 1 fi if [ -f $1 ] then echo "$1 file exist" else echo "Sorry, $1 file does not exist" fi
10
Example 6 #! /bin/bash TEST_DIR=/neuro/labs/gaablab if [ -d "$TEST_DIR" ]; then echo $TEST_DIR already exists. Exiting! exit fi
11
Example 7 x=/neuro/labs/test.nii.gz if [ ! -f "$x" ]; then Echo $x cannot be found exit Fi y=abc echo "$y" if [ -z "$y" ]; then echo yes fi
12
Warnings Don’t use “rm” and “rm -r” in your script when you’re not experienced programmer. Once, a student accidentally used “rm” command and deleted all the data collected over couple of years. THAT’S DISASTER.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.