10.07.2015 Views

Unix Commands

Unix Commands

Unix Commands

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

<strong>Unix</strong> <strong>Commands</strong>man – displays manual entries for a particular command e.g man lsapropos – used when you are unsure of manual (man) entry requireddate - displays current date and timecal 2010 – displays the calendar for 2010-03-16du – displays the number of Kbytes used to store data in filesdf – disk free – displays the amount of free disk spacemore file1 – displays file1, one screenful at a timewhoami – displays who you arewhereis – used to list the locations of program binaries, related files and man pages.ls – displays contents of current directoryls – al – displays contents in long form plus hidden files (-a)ls k* - displays files beginning with kls *.f - displays files ending with .fcat -used to create, display and concatenate filescat file1 – displays contents of file1 to outputcat file1 file2 >file3 – copies file1 and file 2 to file 3cat >file1This is the first line of file1This is the second line of file1CTRL D – terminates inputcp file1 file2 – copies file1 to file2cp –i file1 file2 – prevents file2 from being overwrittenmv file1 file2 – renames file1 as file2rm file1 – removes file1mkdir mysubdir– makes a new directory (note no spaces allowed in UNIX)cd mysubdir – change to new directorypwd – print working directorytouch newfile.txt - creates a file known as "newfile.txt", if the file does not already exist. If the filealready exists the accessed / modification time is updated for the file newfile.txtRedirectionwho > people – result of the who command is sent to the file peopleprogname < file1 – if you have written a program to read characters from the keyboard, it could alsoread characters from a file.1


Pipeswho|sort - is equivalent to:who>tmpfilesort


Access. You can use ping to see if you can reach another computer.Time & distance. You can use the ping command to determine how long it takes to bounce a packetoff of another site, which tells you its Internet distance in network terms.Domain IP address. You can use the ping command to probe either a domain name or an IP address. Ifyou ping a domain name, it helpfully displays the corresponding IP address in the response.ArithmeticTry the following$ expr 2 + 1$ expr 7 – 2$ expr 4 * 5The last one produces an error message as the multiplication operator needs to be quoted (preceded by a \ ) toprotect it from shell expansion. This is because the * is a metacharacter, which have a special meaning to theshell, such as < > * ? | &.$ expr 8 / 5$ expr 19 % 5$ expr 6 + 7 \* 4$ expr `expr 6 + 7` \* 4Variables$ FIRST=John$ echo $FIRSTThis creates a variable named FIRST, sets it’s value to John. To read the value of the variable you must precedeit’s name with a $ symbol.To remove a variable use the unset command as in:$ echo $FIRSTJohn$ unset FIRST$ echo $FIRST$Shell Scriptsecho $SHELL – echoes which shell we are runningCreate a file (first.sh) as follows:#!/bin/bash# This is a comment!echo Hello World3


The first line tells <strong>Unix</strong> that the file is to be executed by the /bin/bash shell.$ chmod 755 first.sh$ ./first.shHello World$Now a second script#!/bin/bas#This script displays the date, time, username and current directoryecho date and time is :dateechoecho your username is : `whoami`echo your current directory is:pwdNotice the back quotes (` `) are used for command substitution in a line commencing echo. The back quote isstill often used for command substitution, although the $() combination (a dollar sign and brackets) is generallypreferred.Creating a personal bin directoryAll executable files must be executed in a bin directory and the path to that directory included in your.bash_profile file.To create a bin directory in your area type:$ mkdir binensuring it is where you want it and you know the absolute pathname.You will probably need to type:$ ls – alto see hidden files which are preceded by a dot.Save a copy of your .bash_profile file to another file. E.g.$ cp .bash_profile bash_profile_copyThen using vi or similar open the .bash_profile file which should be in your area.Then add the following line to the end of the file:PATH=${PATH}:absolute-pathname-to-binexport PATHYou will need to re-boot Linux to re-run the .bash_profile otherwise the PATH won’t be recognised.4


Simple constructsTry the following:1.#!/bin/bash# this program counts from 1 to 10:for i in 1 2 3 4 5 6 7 8 9 10; doecho $idone2.3.#!bin/bashfor i in `seq 1 10`; doecho item: $idone#!bin/bashfor i in $( ls); doecho item: $idoneThe test commandTry the following$ test 3 -gt 4 && echo True || echo falsefalse$ [ "abc" != "def" ];echo $?0Where echo $? returns the exit status of the command. Zero means the command executed successfully, if exitstatus returns non-zero value then your command failed to execute.In the first example the -gt operator performs an arithmetic comparison between two literal values. In thesecond example, the alternate [ ] form compares two strings for inequality. Notice that the spaces after theopening bracket and before the closing bracket are a MUST.You can compare arithmetic values using one of -eq, -ne, -lt, -le, -gt, or -ge, meaning equal, not equal,less than, less than or equal, greater than, and greater than or equal, respectively.Examples1.#!bin/bashread xwhile [ “$x” ]doprintf “%20s\n” $xread x2.done#!bin/basht1=”bog”t2=”off”5


if [ “$t1“ = “$t2“ ]; thenecho expression evaluated as trueelseecho expression evaluated as falsefi3. Then change T2 to bog and see if it evaluates correctly4.#!bin/bashecho Enter aread aecho Enter bread becho Enter cread ctotal=$(expr `expr $a + $b + $c` / 3)echo The mean is $totalecho $aecho $becho $cif ([ $c –gt $b ] && [ $a –lt $b ]);thenecho b = $b and is the medianelif ([ $a –gt $c ] && [ $b –lt $c ]);thenecho c = $c and is the medianelif ([ $b –gt $a ] && [ $c –lt $b ]);thenecho a = $a and is the medianelseecho you have not entered 3 different values#!bin/bashecho Enter your passwordread passwdif [ passwd = “fred” ];thenecho Type Y or Nread keypresscase $keypress iny|Y) echo YES ;;n|N) echo NO ;;*) echo Erroresac6


a) syntactic| pipe symbolAppendix – Metacharacters and reserved words&& `andf' symbol|| `orf' symbol; command separator;; case delimiter& background commands( ) command grouping< input redirection output creation>> output appendb) patterns* match any character(s) including none? match any single character[...] match any of the enclosed charactersc) substitution${...} substitute shell variable`...` substitute command outputd) quoting\ quote the next character'...' quote the enclosed characters except for '"..." quote the enclosed characters except for $ ` \ "e) reserved wordsif then else elif ficase in esacfor while until do done{ }7

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!