分類彙整: Shell

Notes – LSST Ch02 – Part 3

Adopt from

Linux Shell Scripting Tutorial (LSST) v1.05r3

http://www.freeos.com/guides/lsst/ch01sec07.html


Elements in Command

http://www.freeos.com/guides/lsst/ch02sec13.html

name of command + arguments to this command

for example, ls –l

Importance of Command Line Arguments

http://www.freeos.com/guides/lsst/ch02sec14.html

Allow user to use the selected functions on a target

Using $0, $1, $2, $#, $* and $@

http://www.freeos.com/guides/lsst/ch02sec14.html

$ vi demo
#!/bin/sh
#
# Script that demos, command line args
#
# $0, $1 and $2 are the positional parameters
# $0 = “abc” is wrong
echo "Total number of command line argument are $#"
echo "$0 is script name"
echo "$1 is first argument"
echo "$2 is second argument"
echo "All of them are :- $* or $@"

Redirection of Standard output/input i.e. Input - Output redirection

http://www.freeos.com/guides/lsst/ch02sec15.html

Linux-command > filename
To output Linux-commands result (output of command or shell script) to file. Note that if file already exist, it will be overwritten else new file is created.

Linux-command >> filename
To output Linux-commands result (output of command or shell script) to END of file. Note that if file exist , it will be opened and new information/data will be written to END of file, without losing previous information/data, And if file is not exist, then new file is created.

Linux-command < filename
To take input to Linux-command from file instead of keyboard.

Note that ordering is NOT important

Notes – LSST Ch 02 – Part 2

Adopt from

Linux Shell Scripting Tutorial (LSST) v1.05r3

http://www.freeos.com/guides/lsst/ch01sec07.html


Read Statement

echo "What is your favourite sports? "

read sport

echo "Your favourite sport is $sport"

Remember to chmod 755 filename

Wild cards (Filename Shorthand or meta Characters)

*   wild card of a group of characters

?   wild card of a single character

[…]   wild card of any one of the enclosed characters

[...-...]   wild card of a range of characters

[!...-...]   If the first character following the [ is a ! ,then any character not enclosed is matched.

[^...-...]   If the first character following the [ is a ^ ,then any character not enclosed is matched.

More than one command on each command line

semi-colon ; does the trick.

Notes – LSST Ch 02 – Part 1

Adopt from

Linux Shell Scripting Tutorial (LSST) v1.05r3

http://www.freeos.com/guides/lsst/ch01sec07.html

LinuxCommand.org

http://linuxcommand.org

Linux and UNIX command help

http://www.computerhope.com/unix/uwc.htm


How to write a script

http://www.freeos.com/guides/lsst/ch02sec01.html

NOTE In the last syntax ./ means current directory, But only . (dot) means execute given command file in current shell without starting the new copy of shell, The syntax for . (dot) command is as follows

wc – word count

Variables in Shell

http://www.freeos.com/guides/lsst/ch02sec02.html

set

COLORS  /etc/DIR_COLORS
KDE4_LIBDIR     /usr/lib
PRELINKING      yes
_
addsuffix
argv    ()
autolist
autologout      60
csubstnonl
cur_os  Linux
cur_ver 2.6.26.3-29.fc9.i686
cwd     /data2/ie09/wpk009
dirstack        /data2/ie09/wpk009
echo_style      both
edit
gid     20014
group   ie_student
histchar        !
histdup erase
history 20
home    /data2/ie09/wpk009
killring        30
loginsh
lpath   (/usr/ccs/bin /usr/local/cxterm/bin /usr/openwin/bin)
mail    /usr/spool/mail/wpk009
modules_shell   tcsh
notify
owd     /data2/ie09/wpk009
path    (/usr/local/bin /usr/ccs/bin /usr/local/cxterm/bin /usr/openwin/bin /data2/ie09/wpk009/bin /usr/local/java/bin /bin /usr/bin /usr/sbin /usr/ccs/bin /usr/dt/bin . /usr/ucb)
prompt  lx1:/misc/data2/ie09/wpk009>
prompt2 %R?
prompt3 CORRECT>%R (y|n|e|a)?
promptchars     $#
savehist        (1024 merge)
shell   /bin/csh
shlvl   1
sourced 0
status  0
tcsh    6.15.00
term    vt100
tty     pts/0
uid     23717
user    wpk009
version tcsh 6.15.00 (Astron) 2007-03-03 (i386-intel-linux) options wide,nls,dl,al,kan,sm,rh,color,filec

$VARIABLE_NAME

User defined variable(UDV)

http://www.freeos.com/guides/lsst/ch02sec03.html

Variable Naming Rules

http://www.freeos.com/guides/lsst/ch02sec04.html

no space on either side of assignment operator(=)

Print or access value of UDV(like PHP)

http://www.freeos.com/guides/lsst/ch02sec05.html

$variablename

echo $variablename

echo Command

http://www.freeos.com/guides/lsst/ch02sec06.html

echo [options] [string, variables...]
Displays text or variables value on screen.
Options
-n Do not output the trailing new line.
-e Enable interpretation of the backslash escaped characters in the strings

Colorful Text on Screen

http://www.freeos.com/guides/lsst/misc.htm#colorfunandmore

expr Command

http://www.freeos.com/guides/lsst/ch02sec07.html

expr op1 math-operator op2

Spaces are important

e.g.

$ echo `expr 6 % 3`

Black quotes are used.

Quotes (3 Types)

http://www.freeos.com/guides/lsst/ch02sec08.html

"

Double Quotes

"Double Quotes" - Anything enclose in double quotes removed meaning of that characters (except \ and $).

e.g.

echo “Username is $USER”

'

Single quotes

'Single quotes' - Enclosed in single quotes remains unchanged.

`

Back quote

`Back quote` - To execute command

echo “Today is `date`”

Exit Status (return value)

return value is 0 –> the command run successfully.

otherwise –> not successful or some error during execution

Special varaible $? to check the exit status.

more about sheel built in variable: http://www.freeos.com/guides/lsst/misc.htm#moreaboutvari

Notes – LSST Ch 01

Adopt from

Linux Shell Scripting Tutorial (LSST) v1.05r3

http://www.freeos.com/guides/lsst/ch01sec07.html

LinuxCommand.org

http://linuxcommand.org


What kernel is?

http://www.freeos.com/guides/lsst/ch01sec06.html

What is Linux Shell?

http://www.freeos.com/guides/lsst/ch01sec07.html

$ cat /etc/shells

Find all available shells in the system

/bin/sh

/bin/bash

/sbin/nologin

/bin/zsh

/bin/csh

$ echo $SHELL

Find your current shell type

/bin/csh