作者 主題: Lpi 102 Task Oriented Topic 109: Shells, Scripting, Programming and Compiling  (閱讀 4276 次)

0 會員 與 1 訪客 正在閱讀本文。

小徒兒

  • 區域板主
  • 鑽研的研究生
  • *****
  • 文章數: 622
    • 檢視個人資料
***customize shell environments to meet users' needs
vi /etc/rc.d/rc.sysinit
代碼: [選擇]

for tty in /dev/tty[1-9]*;do
  setleds -D +num < $tty
done


vi /etc/rc.local
vi /etc/profile
vi ~/.bash_profile
代碼: [選擇]

 /usr/bin/tty | egrep -q 'tty?' && export LANG=C


***setting environment variables (e.g. PATH) at login or when spawning a new shell
vi /etc/bashrc
vi ~/bashrc
代碼: [選擇]

alias lc="export LANG=C;locale"
alias hi="history | tail -100 "




*** writing bash functions for frequently used sequences of commands.
代碼: [選擇]

function show-homedir-comment { cat /etc/passwd | ( IFS=: ; while read lognam pw id gp fname home sh;do echo $home \"$fname\"; done ) }


代碼: [選擇]

 function cp-pattern { for i in $1; do cp $i ${i%$1}$2; done


cp-pattern "*.txt"




***~/.bash_profile #/etc/profile
The system-wide initialization file; executed when you log in.

***~/.bash_login
If .bash_profile doesn't exist, this file is executed automatically when you log in.

***~/.profile  
If neither .bash_profile nor .bash_login exists, this file is executed automatically when you log in.

***~/.bashrc
This file is executed automatically when bash starts.

***~/.bash_logout
This file is executed automatically when you log out.

***~/.inputrc #/etc/inputrc
This file contains optional key bindings and variables that affect how bash responds to your keystrokes.

***function (Bash built-in command)
代碼: [選擇]
bash,  :,  ., [, alias, bg, bind, break, builtin, cd, command, compgen,
       complete, continue, declare, dirs, disown, echo,  enable,  eval,  exec,
       exit,  export,  fc,  fg, getopts, hash, help, history, jobs, kill, let,
       local, logout, popd, printf, pushd, pwd, read, readonly,  return,  set,
       shift,  shopt,  source,  suspend,  test,  times,  trap,  type, typeset,
       ulimit, umask, unalias, unset,  wait  -  bash  built-in  commands,  see


type echo
man 1 set
man 1 bash

***export
環境變數宣告 (bash shell 內建指令)
if [ "3" = $(runlevel | cut -d " " -f 2) ] ;then  export LANG=C; fi

***env
show the environment variable

***set
show all variable in this shell


$set -o vi #The default line-edit mode is Emacs. To enable vi mode
$set -o noclobber       #Prevent file overwriting
$set +o noclobber       #Allow file overwriting again

$set -x     #enable command tracing,Show commands and arguments when executed
$set +x     #Stop command tracing

$set -- "$num" -20 -30   #Set $1 to $num, $2 to -20, $3 to -30


***unset (Bash built-in command) #remove the var unset i
$unset i

小徒兒

  • 區域板主
  • 鑽研的研究生
  • *****
  • 文章數: 622
    • 檢視個人資料
1.109.2 Customize or write simple scripts
« 回覆 #1 於: 2004-09-15 07:44 »
***using standard sh syntax (loops, tests)

#while loop read a stream
代碼: [選擇]

while read input
  do
     if [ ${#input} -gt $width ] ; then
       echo "$input" | fmt
     else
       echo "$input"
     fi
  done < $1

 ???

if [ ${#input} -gt $width ] ; then
if test ${#input} -gt $width ; then


***using command substitution
代碼: [選擇]

if [ $# -eq 0 -o ! -f "$1" ] ; then
  echo "Usage: [color=red]$(basename $0) [/color]logfile" >&2
  exit 1
fi



***testing command return values
代碼: [選擇]

/usr/bin/tty | egrep -q 'tty?'
echo $?


***testing of file status
-d file #True if file exists and is a directory

-e file #True if file exists

-f file #True if file exists and is a regular file

-L file #True if file exists and is a symbolic link

-r file #True if file exists and is readable

-s file #True if file exists and has a size greater than zero

-w file #True if file exists and is writable

-x file #True if file exists and is executable

file1 -ot file2 #True if file1 is older than file2


***conditional mailing to the superuser ???


***correct interpreter is called on the first (#!) line of scripts
#!/usr/bin/wish ???


***managing location, ownership, execution and suid-rights of scripts
PATH
chown
ls -la
chmod u+x
chmod u+s


***for
???
代碼: [選擇]

for arg
do
  newname="$mydir/$(date "+%S.%M.%H.%d.%m").$(basename "$arg")"
  if [ -f "$arg" ] ; then
    $copy "$arg" "$newname"
  elif [ -d "$arg" ] ; then
    $copy "$arg" "$newname"
  fi
done


***test

-z file #if file exit and not null content

-z string #True if the length of string is zero

-n string #True if the length of string is nonzero

string1 = string2 #True if the strings are equal

string1 != string2 #True if the strings are not equal

num1 -eq num2 #true if num1 equal to num2

num1 -ne num2 #true if nmb1 not equal to num2

num1 -gt num2 #true if num1 greater than num2

-gt greaterthan -lt less than -le less & equal -ge greater & equal

! #mean NOT

-a #mean AND

-o #mean OR