Linuxshell编程学习笔记2.doc_第1页
Linuxshell编程学习笔记2.doc_第2页
Linuxshell编程学习笔记2.doc_第3页
Linuxshell编程学习笔记2.doc_第4页
Linuxshell编程学习笔记2.doc_第5页
已阅读5页,还剩3页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

Linux shell编程学习笔记(二)-chinaitlab linux学习视频2008-03-07 00:31第三章 shell的输入和输出1.echo echo option string -e 解析转移字符 -n 回车不换行,linux系统默认回车换行 转移字符 c t f n#!/bin/bash#echoecho -e this echos 3 newlnennnecho OKechoecho this is echos 3 ewlinennnecho this log file have all been donemylogfile.txttestszbirdora $ sh echod.shthis echos 3 newlneOKthis is echos 3 ewlinennn上面可以看到有-e则可以解析转移字符,没有不能解析。echo空输出为空2.read 可以从键盘或文件的某一行文本中读入信息,并将其赋给一个变量read variable1 variable2eg.#!/bin/bash#readnameecho -n first name:read firstnameecho -n last name:read lastnameecho this name is $firstname $lastname3.cat 显示文件的内容,创建内容,还可以显示控制字符 cat optionsfilename1 filename2 -v 显示控制字符(Windows文件) cat命令不会分页显示,要分页可以采用more、less4.管道|5.tee把输出的一个副本输送到标准输出,另一个副本拷贝到相应的文件中,一般与管道合用 tee options files -a 在文件中追加eg.testszbirdora 1$ echo |tee myfiletestszbirdora 1$ cat myfile将myfile文件置空6.文件重定向commandfilename -覆盖输出 commandfilename -追加输出commandfilename&1 -把标准输出和标准错误重定向commanddelimiter -输入直到delimiter分解符commandfilename-输入文件内容到命令commandnullfile.txt -创建字节为0的文件command1command3 -按从左到右顺序执行eg.说明:myfile为空间testszbirdora 1$ df -lhmyfiletestszbirdora 1$ cat myfileFilesystem Size Used Avail Use% Mounted on/dev/sda1 20G 3.3G 16G 18% /none 2.0G 0 2.0G 0% /dev/shm/dev/sda2 79G 17G 59G 23% /u01/dev/sda4 28G 3.9G 22G 15% /u02testszbirdora 1$ df -lhmyfiletestszbirdora 1$ cat myfileFilesystem Size Used Avail Use% Mounted on/dev/sda1 20G 3.3G 16G 18% /none 2.0G 0 2.0G 0% /dev/shm/dev/sda2 79G 17G 59G 23% /u01/dev/sda4 28G 3.9G 22G 15% /u02testszbirdora 1$ df -lhmyfiletestszbirdora 1$ cat myfileFilesystem Size Used Avail Use% Mounted on/dev/sda1 20G 3.3G 16G 18% /none 2.0G 0 2.0G 0% /dev/shm/dev/sda2 79G 17G 59G 23% /u01/dev/sda4 28G 3.9G 22G 15% /u02Filesystem Size Used Avail Use% Mounted on/dev/sda1 20G 3.3G 16G 18% /none 2.0G 0 2.0G 0% /dev/shm/dev/sda2 79G 17G 59G 23% /u01/dev/sda4 28G 3.9G 22G 15% /u02testszbirdora 1$ cat myfile China Hubei Suizhou exittestszbirdora 1$ cat myfileChinaHubeiSuizhou7.exec 可以用来替代当前shell。现有任何环境变量都会清除第四章 控制流结构1.if语句if 条件1then 命令1elif 条件2then 命令2else 命令3fi-if 条件then 命令fieg:#!/bin/bash#if test#this is a comment lineif 10 -lt 12 ;then#yes 10 is less than 12echo yes,10 is less than 12elseecho nofi注意:if语句必须以fi终止 10 前一个空格,“12”后也有一个空格。这个条件都是通过test命令来指定。条件表达为test expression或者expression条件表达式中的比较函数man testNAME test - check file types and compare valuesSYNOPSIS test EXPRESSION EXPRESSION OPTIONDESCRIPTION Exit with the status determined by EXPRESSION. -help display this help and exit -version output version information and exit EXPRESSION is true or false and sets exit status. It is one of: ( EXPRESSION ) EXPRESSION is true ! EXPRESSION EXPRESSION is false EXPRESSION1 -a EXPRESSION2 both EXPRESSION1 and EXPRESSION2 are true EXPRESSION1 -o EXPRESSION2 either EXPRESSION1 or EXPRESSION2 is true -n STRING the length of STRING is nonzero -z STRING the length of STRING is zero STRING1 = STRING2 the strings are equal STRING1 != STRING2 the strings are not equal INTEGER1 -eq INTEGER2 INTEGER1 is equal to INTEGER2 INTEGER1 -ge INTEGER2 INTEGER1 is greater than or equal to INTEGER2 INTEGER1 -gt INTEGER2 INTEGER1 is greater than INTEGER2 INTEGER1 -le INTEGER2 INTEGER1 is less than or equal to INTEGER2 INTEGER1 -lt INTEGER2 INTEGER1 is less than INTEGER2 INTEGER1 -ne INTEGER2 INTEGER1 is not equal to INTEGER2 FILE1 -ef FILE2 FILE1 and FILE2 have the same device and inode numbers FILE1 -nt FILE2 FILE1 is newer (modification date) than FILE2 FILE1 -ot FILE2 FILE1 is older than FILE2 -b FILE FILE exists and is block special -c FILE FILE exists and is character special -d FILE FILE exists and is a directory -e FILE FILE exists -f FILE FILE exists and is a regular file -g FILE FILE exists and is set-group-ID -h FILE FILE exists and is a symbolic link (same as -L) -G FILE FILE exists and is owned by the effective group ID -k FILE FILE exists and has its sticky bit set -L FILE FILE exists and is a symbolic link (same as -h) -O FILE FILE exists and is owned by the effective user ID -p FILE FILE exists and is a named pipe -r FILE FILE exists and is readable -s FILE FILE exists and has a size greater than zero -S FILE FILE exists and is a socket -t FD file descriptor FD (stdout by default) is opened on a terminal -u FILE FILE exists and its set-user-ID bit is set -w FILE FILE exists and is writable -x FILE FILE exists and is executableeg.#!/bin/bash#if test#this is a comment lineecho Enter your filename:read myfileif -e $myfile then if -s $myfile ;then echo $myfile exist and size greater than zero else echo $myfile exist but size is zero fielseecho file no existfitestszbirdora 1$ sh iftest.sh Enter your filename:1111 exist but size is zero2.case语句case语句为多选择语句。case 值 in模式1) 命令1 ;模式2) 命令2 ;esaceg.#!/bin/bash#case selectecho -n enter a number from 1 to 3:read anscase $ans in1)echo you select 1;2)echo you select 2;3)echo you select 3;*)echo basename $0:this is not between 1 and 3&2exit;esac3.for 循环for循环一般格式:for 变量名 in 列表 (列表以

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论