CS306 Lab Workout 2 Fall 2017
Linux commands workout Find out your IP address: ifconfig Test if your Internet is working: ping www.yahoo.com Introduce vi as the command line editor Show how to write data (in ‘i’ mode) Save as a file (:w) Exit vi (:q) Save and exit (:wq) Exit without saving (:w!)
Linux commands workout Sort contents of a text file using the sort command more big-text_file.txt Display the file called big-text_file.txt one page at a time, proceed to next page using the spacebar head big-text_file.txt Display the first 10 lines of the file called big-text_file.txt head -20 big-text_file.txt Display the first 20 lines of the file called big-text_file.txt tail big-text_file.txt Display the last 10 lines of the file called big-text_file.txt tail -20 big-text_file.txt Display the last 20 lines of the file called big-text_file.txt
Process Life Cycle Diagram
Linux commands workout df –h du -h Create a directory structure ./test/child/tmp: mkdir –p ./test/child/tmp Change to ./test First create a file called “junk” in the CWD (touch ./junk). Then, create a hard link to it called junk-hardlink using: ln ./junk junk-hardlink ln -s ../junk junk-alias Create a hard link to the directory ../test: ln ../test test-link Create a soft link to the directory ../test: ln –s ../test test-softlink Use stat to display the inode structure and link count for test directory File test-link File junk File junk-hardlink File junk-alias
Linux commands workout top ps ps –e ps –ex kill -9 background process using & and then fg pipe operator: ls –l|wc|sort –r printenv
Linux Special Characters Expansions echo D* echo ~ echo $((2 + 2)) mkdir {2007..2009}-0{1..9} {2007..2009}-{10..12} ls -l $(which cp) find /bin -name “*gc*” grep "this" index.html grep -c "this" index.html
Linux Special Variables Description $0 The filename of the current script. $n These variables correspond to the arguments with which a script was invoked. Here n is a positive decimal number corresponding to the position of an argument (the first argument is $1, the second argument is $2, and so on). $# The number of arguments supplied to a script. $* All the arguments are double quoted. If a script receives two arguments, $* is equivalent to $1 $2. $@ All the arguments are individually double quoted. If a script receives two arguments, $@ is equivalent to $1 $2. $? The exit status of the last command executed. $$ The process number of the current shell. For shell scripts, this is the process ID under which they are executing. $! The process number of the last background command.
Linux Special Variables Create a text file using: vi test.sh Press “i” key on keyboard to enter “insert” mode [This allows you to type] Enter the following lines: #!/bin/sh echo "File Name: $0" echo "First Parameter : $1" echo "Second Parameter : $2" echo "Quoted Values: $@" echo "Quoted Values: $*" echo "Total Number of Parameters : $#“ Press Esc, followed by wq and Enter – This saves and exits vi chmod 755 test.sh Execute ./test.sh this is a “test program”