shell programing

$ /bin/bash –version

Pipes and Redirection

$ ls -l > lsoutput.txt

file descriptor 0 is the standard input
file descriptor 1 is the standard output
file descriptor 2 is the standard error

set cannot overwrite existing file
$ set -o noclobber
cancel
$ set +o noclobber

append to the file

$ ps >> lsoutput.txt

examples:

$ kill -HUP 1234 >killout.txt 2>killerr.txt

$ kill -1 1234 >killouterr.txt 2>&1

$ kill -1 1234 >/dev/null 2>&1

$ more < killout.txt

$ ps | sort > pssort.out

never use the same filename twice in a string of commands.

cat mydata.txt | sort | uniq > mydata.txt


Interactive Programs

 $ for file in *
 > do
 > if grep -l POSIX $file
 > then
 > more $file
 > fi
 > done 
 posix
 This is a file with POSIX in it - treat it well 
 $

通配符:
[set] – single character , [^set] negates the set
list the files my_fingers and my_toes

ls my_{finger,toe}s

$ more `grep -l POSIX *`
$ more $(grep -l POSIX *)
/* file contain POSIX */
$ grep -l POSIX * | more

Creating a Script

the #! characters tell the system that the argument that follows on the line is the program to be used to execute this file. In this case, /bin/sh is the default shell program.

filename : first

#!/bin/sh

# first
# This file looks through all the files int the current
# directory for the string POSIX, and then prints the names of
# those files to the standard output.

for file in *
do
  if grep -q POSIX $file
  then
    echo $file
  fi
done

exit 0

check if they are scripts or not

$ file first
first: POSIX shell script, ASCII text executable

Making a Script Executable

  • $ /bin/sh first

  • $ chmod +x first

either type PATH=$PATH:. on the command line or edit your .bash_profile file to add this command to the end of the file; then log out and log back in again. Alternatively, type ./first in the directory.

for your own use, you could create a bin directory in your home directory and add that to your path.
If you want the script to be executable by others, you could use /usr/local/bin or another system directory as a convenient location for adding new programs.

be safeful use

# cp first /usr/local/bin
# chown root /usr/local/bin/first
# chgrp root /usr/local/bin/first
# chmod 755 /usr/local/bin/first
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值