shell programing
2015-08-29 14:23
453 查看
$ /bin/bash –version
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
通配符:
[set] – single character , [^set] negates the set
list the files my_fingers and my_toes
ls my_{finger,toe}s
filename : first
check if they are scripts or not
$ file first
first: POSIX shell script, ASCII text executable
$ 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
Pipes and Redirection
$ ls -l > lsoutput.txtfile 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
相关文章推荐
- 使用shell操作数据库
- Shell 种类
- linxu shell iconv 转码问题
- LinuxDeepin/Unix、Linux shell命令
- shell练习五
- Spark的Python和Scala shell介绍(翻译自Learning.Spark.Lightning-Fast.Big.Data.Analysis)
- Linux生产服务器Shell脚本分享
- hive shell命令
- 希尔排序(shell)
- Shell 脚本面试问题大全
- 一个Linux中用于监控的简易shell脚本
- 关于shell中的正则表达式和awk的使用
- SSH隧道管理内部服务(putty,ssh secure shell)for win
- adb 连不上设备的种种情况
- bash: ifconfig: command not found 解决办法
- Shell scripts to Create a local dir base on the time.
- 小议:如何用PowerShell创建State Services?
- HBase shell 无法使用 Backspace 键-解决方案
- linux shell if 参数
- linuxSHELL学习之获取用户输入