(2013-01-18 15:28)
本章将简单描述BASH脚本如何获得命令行参数以及相关的变量。
以下是脚本执行的过程中默认的几个变量,可用来获得参数以及相关信息:
$# //The script parameter
count,当前脚本在执行时一共接受了几个参数
$0, $1, $2, $3 ........ //The script parameter
values,当前脚本在执行时的第1,2,3个参数值
$$ //Current script
PID,当前脚本执行时的进程ID
$? //The last command execute
status 0 means successful.上一个命令退出时的返回值
以下是脚本示例代码(红色部分):
#!/bin/bash
echo 'Current script PID is $$'
echo 'Current script parameter count is : $#'
echo 'Script execute string is : $0'
echo 'First parameter is : $1'
echo 'Second parameter is : $2'
echo 'Third parameter is : $3'
ping -c 1
(2013-01-18 15:05)
这一章将简单的讲述BASH脚本编程里面的变量定义。
在BASH脚本编程里面的变量可以使用:变量名=变量值 的方式定义。
例如:
Username='Oracle'
ID='4094958'
Age=30
在定义变量的时候,直接使用 变量名=变量值 方式定义的变量作用域是整个脚本环境,包括脚本中的自定义函数。
如果您需要在自定义函数中使用私有的变量(即变量的作用域只局限于函数内部),则您可以使用 local
关键字。
例如:
local Username='Solaris'
下面是脚本示例代码:
#!/bin/bash
UserName='Oracle'
ID='123456'
Age=30
LocalFunction()
{
local UserName='Solaris'
local ID='ABCDEF'
local Age=25
echo 'Local UserName: $UserName'
echo 'Local ID :
$ID'
很多同学问我怎么学习Bash脚本编程,今天就把以前给同事培训的一点资料贴出来,已期望能够帮助到想学习Bash脚本编程的同学。
在脚本编程学习的过程中,需要您至少已经会使用基础的LINUX/UNIX命令,至少ls, chmod, chown,
vi等命令行工具能够使用。
如果上面的三个命令您还不会用,您需要先学会了再来看这个资料。
下面是一个Hello World的代码,让我们开始学习BASH脚本编程吧。
1.使用touch命令在LINUX/UNIX系统中创建一个普通文件,文件名为: HelloWorld.sh
BASH# touch ./HelloWorld.sh
2.使用vi命令编辑HelloWorld.sh文件,输入下面的代码(红色部分),并保存文件。
BASH# vi ./HelloWorld.sh
#!/bin/bash
Message='Hello World!'
echo '$Message'
3.使用chmod命令给予文件可以执行的权限
BASH# chmod 700 ./HelloWorld.sh
4.执行脚本,并查看输出
#!/usr/bin/perl
#Define develop information here
$ApplicationVerion='1.0';
$ApplicationCopyright='All Right reserved';
$ApplicationAuthor='Anthony Zhao';
$ApplicationAuthorEmail='Jones.Zhao\@Gmail.Com';
#Import additional module here
use Getopt::Std;
use Crypt::OpenSSL::RSA;
#use Convert::PEM;
use MIME::Base64;
#Generate application run time variable here
$ApplicationExecutePathAndName=$0;
###For Unix File System
$ApplicationExecutePathAndName=$0;
$ApplicationExecuteName=$0;$ApplicationExecuteName=~s/^.*\///;
$ApplicationExecutePath=$0;$ApplicationExecutePath=~s/$ApplicationExecuteName$//;
$ApplicationLogFile=$ApplicationExecutePathAndName.'.log';
#Define global constant here
#Define global variable here
$KeyFileName='';
$KeyData='';
$Data='';
#$public_key='Key.Public.PKCS1';
$public_key='Key.Public.X509';
$private_key='Key.Priva
#!/usr/bin/perl
#Define develop information here
$ApplicationVerion='1.0';
$ApplicationCopyright='All Right reserved';
$ApplicationAuthor='Anthony Zhao';
$ApplicationAuthorEmail='Jones.Zhao\@Gmail.Com';
#Import additional module here
use Getopt::Std;
use Crypt::OpenSSL::Random;
use Crypt::OpenSSL::RSA;
#Generate application run time variable here
$ApplicationExecutePathAndName=$0;
###For Unix File System
$ApplicationExecutePathAndName=$0;
$ApplicationExecuteName=$0;$ApplicationExecuteName=~s/^.*\///;
$ApplicationExecutePath=$0;$ApplicationExecutePath=~s/$ApplicationExecuteName$//;
$ApplicationLogFile=$ApplicationExecutePathAndName.'.log';
#Key File Name to Save
$PrivateKeyFile='./Key.Private';
$PublicKeyPKCS1File='./Key.Public.PKCS1';
$PublicKeyX509File='./Key.Public.X509';
#Define global constant here
#Define global variable here
$KeyFileN
(2012-09-03 10:56)
示例:
if(-e 'File-Name')
{
print 'TRUE\n';
}
else
{
print 'FALSE\n';
}
检测选项 含义
-r 文件或目录对此(有效的)用户(effective user)或组是可读的
-w 文件或目录对此(有效的)用户或组是可写的
-x 文件或目录对此(有效的)用户或组是可执行的
-o 文件或目录由本(有效的)用户所有
-R 文件或目录对此用户(real user)或组是可读的
-W 文件或目录对此用户或组是
(2012-09-03 10:44)
$Buffer='';
##hostname为需要运行的外部程序,注意使用反引号
$Buffer=`hostname`;
print $Buffer.'\n';
#############################
$Buffer='';
@DataBuffer='';
$Buffer=`ifconfig -a`;
@DataBuffer=split('\n',$Buffer);
for($i=0;$i
{
print $DataBuffer[i].'\n';
}
(2012-09-03 10:43)
##################################
按行读取文件
##################################
open(FILE,'filename') || die 'can not open the file: $!';
while (defined ($eachline =))
{
chomp $eachline;
print $eachline.'\n';
}
close FILE;
##################################
把文件全部读到变量里面
##################################
$AllData='';
open(FILE,'filename');
read(FILE,$AllData,-s FILE); # Suck in the whole file
close(FILE);
##################################
把文件全部读到内存里面-01
##################################
open(FILE,'filename') || die'can not open the file: $!';
@filelist=;
foreach $eachline (@filelist)
{
chomp $eachline;
}
close FILE;
##################################
把文件全部读到内存里面
#!/usr/bin/perl
#Define develop information here
$ApplicationVerion='1.0';
$ApplicationCopyright='All Right reserved';
$ApplicationAuthor='Anthony Zhao';
$ApplicationAuthorEmail='Jones.Zhao\@Gmail.Com';
#Import additional module here
use Getopt::Std;
#Generate application run time variable here
$ApplicationExecutePathAndName=$0;
###For Unix File System
$ApplicationExecutePathAndName=$0;
$ApplicationExecuteName=$0;$ApplicationExecuteName=~s/^.*\///;
$ApplicationExecutePath=$0;$ApplicationExecutePath=~s/$ApplicationExecuteName$//;
$ApplicationLogFile=$ApplicationExecutePathAndName.'.log';
#Define global constant here
#Define global variable here
%ApplicationOptions=();
#Define function here
#Function GetCurrentTime, using to get current time string.
Usage: GetCurrentTime();
sub GetCurrentTime
{
my ($sec,$min,$hour,$day,$mon,$year,$w
#!/usr/bin/perl
#Define develop information here
$ApplicationVerion='1.0';
$ApplicationCopyright='All Right reserved';
$ApplicationAuthor='Anthony Zhao';
$ApplicationAuthorEmail='Jones.Zhao\@gmail.com';
#Import additional module here
use Getopt::Std;
#Generate application run time variable here
$ApplicationExecutePathAndName=$0;
$ApplicationExecuteName=$0;$ApplicationExecuteName=~s/^.*\\//;
$ApplicationExecutePath=$^X;
$ApplicationLogFile=$ApplicationExecutePathAndName.'.log';
#Define global constant here
#Define global variable here
%ApplicationOptions=();
#Define function here
#Function GetCurrentTime, using to get current time string.
Usage: GetCurrentTime();
sub GetCurrentTime
{
my
($sec,$min,$hour,$day,$mon,$year,$weekday,$yeardate,$savinglightday)=(localtime(time));
$sec = ($sec < 10)? '0$sec':$sec;
$min = ($min < 10)?