Monday, December 31, 2012

Beginning Shell Scripting

Beginning Shell Scripting
1.
Linux and Mac OS X systems actually use the bash (Bourne Again) shell as the default shell. Bash then masquerades as sh, the Bourne shell. But on standards-compliant Unix systems such as Solaris, from Sun Microsystems, the sh command is supposed to be the Korn shell, ksh (covered following).
Unless you are the administrator or have administrator permissions, you are stuck with the default shell as defined by your administrator. That's usually okay because modern shells are all pretty good, and you may need to deal with assumptions regarding shells as you work.
to change user ericfj to use bash, run the chsh command as follows:

$ chsh ericfj /bin/bash

Note that you need to enter the full path to the shell. The chsh command will likely require you to type in your password, so that only you and the administrator can change your default shell. The new login shell will be available for use the next time you log in. (You can log out and log back in to run the new shell.)
On Linux, you need a slightly different syntax:

chsh -s new_default_shell username

On BSD Unix, use the chpass command, which requires the following syntax:
chpass -s new_default_shell username
if your default shell is bash, but you want to try the features of tcsh, simply type in tcsh and try it out:

$ tcsh

2.
The Korn shell supports a command history as well but uses the r command instead of an exclamation mark. Like the C shell, you may need to set up the history feature first, but set the HISTSIZE value instead of the C shell's history:

$ HISTSIZE=60

Once set up, run the fc command (short for fix command) to view the history, with the -l (ell) option:

$ fc -l
1      set -o vi
2      fc
3      pwd
4      pwd
5      fc -l

The fc command can also call up an editor.
When you've selected a command, you can execute that command by number or partial text, similar to the feature in the C shell. Remember to use the r command in the Korn shell, as ksh does not support the exclamation-mark syntax.
The bash and ksh shells also support the ability to call up a full-screen editor. The fc command, used previously to list the command history, can call up a text editor. Type fc alone to call up an editor, vi or emacs, on the previous command. Pass a number to fc to edit the given command in the history, such as fc 3 to edit command 3 in the history. Pass some text of a previous command, such as fc cp, to edit the previous command that starts with the given text.

3.
Ksh doesn't support the Tab character for file-name completion.
Press Esc-\ in ksh in vi editing mode. Ksh doesn't support the Tab character for file-name completion. Bash supports Tab or Esc-/.
Esc-= in the Korn shell to list all the possible names.

4.
Tcl, pronounced tickle, stands for Tool Command Language. Tcl was created as a lightweight means to add scripting to applications such as word processors, integrated-circuit testers, and spreadsheets.

5.
You can pass more than one file name on the command line. For example:

$ vi script1 script2 script3

Vi shows each file one at a time. The :n command (see the second table following) jumps to the next file.
:rew
Go back to the first file.
 
The following table lists the commands to switch over to insert mode.

Vi Insert CommandPurpose
iInserts before the current character
IInserts at the beginning of the line
aAppends after (to the right of) the current character
AAppends at the end of the current line
oInserts a new line after the current line
OInserts a new line before the current line

 
Vi Navigation KeyPurpose
hMove one character left.
jMove one line down.
kMove one line up.
lMove one character right.
Ctrl-DMove down one-half page.
Ctrl-UMove up one-half page.
Ctrl-FMove forward one page.
Ctrl-BMove back one page.
GJump to the end of the file.
1GJump to the first line.
number GJump to the given line number.

Vi Copy and Paste CommandUsage
yyYanks current line into buffer, similar to copying to the clipboard.
pPuts the yanked text on the next line after the current position of the cursor.
number yyYanks more than one line, as specified by number.
uUndoes the last command.
rRedo, when called after an undo, performs the undone command again. Not supported by default in vim.
.Repeats the last command.

Vi Search CommandUsage
/textSearch for text going forward.
/Search again for the same text, going forward.
?textSearch for text going backward.
?Search again for the same text, going backward.
nRepeat last search.
NRepeat last search, but reverse the direction.


6.
for filename in *.doc
do
   echo "Copying $filename to $filename.bak"
   cp $filename $filename.bak
done

# Counts by looping for a fixed number of times

for i in 1 2 3 4 5 6 7 8 9 10
do
   echo -n "...$i"
done

echo    # Clean up for next shell prompt


sleep 5

This command causes the shell to wait for five seconds before going on to the next command.

if (sh return0) then
  echo "Command returned true."
else
   echo "Command returned false."
fi


$ ls /fred > /dev/null 2> /dev/null
$

This example redirects the normal output from ls, as well as the error messages.
TestUsage
$s1 -zReturns true if the length of s1 (the number of characters in s1) is zero
$s1 -nReturns true if the length of s1 (the number of characters in s1) is not zero

TestUsage
-d filenameReturns true if the file name exists and is a directory
-e filenameReturns true if the file name exists
-f filenameReturns true if the file name exists and is a regular file
-r filenameReturns true if the file name exists and you have read permissions
-s filenameReturns true if the file name exists and is not empty (has a size greater than zero)
-w filenameReturns true if the file name exists and you have write permissions
-x filenameReturns true if the file name exists and you have execute permissions

 


echo "Please enter your favorite operating system, "
echo -n "linux, macosx, windows, amigados, or beos: "
read os

case $os in

linux)
   echo "Way cool, you like Linux."
;;
macosx)
   echo "You like Roman numerals."
;;
windows)
   echo "Time to check for a virus."
;;
amigados)
   echo "AmigaDOS will never die."
;;
beos)
   echo "Count yourself lucky."
;;
*)
   echo "Why did you choose $os?"
;;
esac


command="init"    # Initialization.

while [ "$command" != "exit" ]
do

   echo -n "Enter command or \" exit\" to quit: "
   read command
   echo

   case $command in
   ls)
       echo "Command is ls."
   ;;
   who)
       echo "Command is who."
   ;;
   *)
       if [ $command != "exit" ]
       then
           echo "Why did you enter $command?"
       fi
   ;;
   esac

done

How the Korn Shell Starts Up

When you run ksh as a login shell, it looks for a file in your home directory named .profile and sources the commands in that file. This is the same as the Bourne shell.
If you run ksh as a non-login shell, the Korn shell looks for a startup file named by the ENV environment variable. This is similar to the C shell's .cshrc file.
If ENV names a directory, the Korn shell looks for a file named ksh_env within that directory. If ENV names a file, the Korn shell loads that file.
ShellStartup FileLogin FileLogout File
ksh$ENV.profileNone

 
By convention, if your script starts with #! (often called a shebang
because it starts with a hash-exclamation point), then the special
comment tells the shell which program should interpret the script.
tar cf myarchive.tar *.doc
tar xf myarchive.tar
gzip myarchive.tar  --By default, the gzip program compresses the given file. It then changes the file name to end with a .gz extension
400Owner has read permission.
200Owner has write permission.
100Owner has execute permission.

Short for no hangup, the nohup
command runs a command and keeps that command running even if you log
out. Typically, when you log out, all the commands you launched are
terminated if they are still running.
Short for disk free, df returns the amount of space used and available on all mounted file systems. With no arguments, df lists information for all mounted file systems.
OptionUsage
-kReturns the output in 1K blocks
-lDisplays information on local file systems only.

Lists the amount of disk space used for a given set of files or directories. Technically, du estimates the amount of disk usage.
OptionUsage
-aPrints a line of output for each file, rather than just one line per directory.
-sSilent mode. Displays only the total line.


find start_at conditions actions
$ find . -ctime -1 -print

Finds all files in the current directory and below that have been modified in the last day. The -print option is not necessary.
$ find $HOME -name 'script*'

Finds all files in the user's home directory (and below) with a name that starts with script. 

$ cat /etc/passwd | sed '2~2d'
$ cat /etc/passwd | sed 's/Root/toor/ig'
$ cat /etc/passwd | sed 's:/root:/toor:' | head -2  =  $ cat /etc/passwd | sed 's/\/root/\/toor/' | head -2
$ cat /etc/passwd | sed '1,5s/sh/quiet/g'
 
By default, awk's behavior is to print the entire line, so each of the following lines results in the same output:

awk '/Al/' countries.txt
awk '/Al/ { print $0 }' countries.txt
awk '/Al/ { print }' countries.txt

awk -F, '{ print $3 }' countries.csv

No comments:

Post a Comment