linux shell中,$是一个函数吗?
在哪里可以看到这个函数的函数体。比如/etc/rc.d/rc中的#First,runtheKILLscripts.foriin/etc/rc$runlevel.d/K*;...
在哪里可以看到这个函数的函数体。
比如 /etc/rc.d/rc中的
# First, run the KILL scripts.
for i in /etc/rc$runlevel.d/K* ; do
check_runlevel "$i" || continue
# Check if the subsystem is already up.
subsys=${i#/etc/rc$runlevel.d/K??}
[ -f /var/lock/subsys/$subsys -o -f /var/lock/subsys/$subsys.init ] \
|| continue
# Bring the subsystem down.
if egrep -q "(killproc |action )" $i ; then
$i stop
else
action $"Stopping $subsys: " $i stop
fi
done
里面就好像是把$当作函数来用的 展开
比如 /etc/rc.d/rc中的
# First, run the KILL scripts.
for i in /etc/rc$runlevel.d/K* ; do
check_runlevel "$i" || continue
# Check if the subsystem is already up.
subsys=${i#/etc/rc$runlevel.d/K??}
[ -f /var/lock/subsys/$subsys -o -f /var/lock/subsys/$subsys.init ] \
|| continue
# Bring the subsystem down.
if egrep -q "(killproc |action )" $i ; then
$i stop
else
action $"Stopping $subsys: " $i stop
fi
done
里面就好像是把$当作函数来用的 展开
展开全部
$不是函数,表示一个变量。
追问
比如 /etc/rc.d/rc中的
# First, run the KILL scripts.
for i in /etc/rc$runlevel.d/K* ; do
check_runlevel "$i" || continue
# Check if the subsystem is already up.
subsys=${i#/etc/rc$runlevel.d/K??}
[ -f /var/lock/subsys/$subsys -o -f /var/lock/subsys/$subsys.init ] \
|| continue
done
里面就好像是把$当作函数来用的
追答
$i就表示变量i的意思。$runlevel就表示变量runlevel的意思。你可以找本shell书简单看看,就明白了。
展开全部
楼主发表于:2009-06-08 23:24:53
myshell.h
#define FALSE 0
#define TRUE 1
#define LINE_LEN 80
#define MAX_ARGS 64
#define MAX_ARG_LEN 16
#define MAX_PATHS 64
#define MAX_PATH_LEN 96
#define WHITESPACE ".,\t&"
#define STD_INPUT 0
#define STD_OUTPUT 1
#ifndef NULL
#define NULL 0
#endif
struct command_t{
char *name;
int argc;
char *argv[MAX_ARGS];
};
myshell.c
#include
#include
#include
#include
#include
#include
#include
char *lookupPath(char **,char**);
int parseCommand(char *,struct command_t*);
int parsePath(char **);
void printPrompt();
void readCommand(char*);
char promptString[]="vivu shell>";//定义shell名为vivu
int main(){//调度整个shell执行的流程
char commandLine[LINE_LEN];
char *pathv[MAX_PATHS];
int numPaths;
int i,j,len;
int chPID;//child pid
int stat;//used by parent wait
int fg;//command in foreground?
int fid;
int pipeID[2];//创建管道
int isInRedir,isOutRedir,isPipe;
pid_t thisChPID;
struct command_t command,command2;
//shell initialization
for(i=0;i<MAX_ARGS;i++){
command.argv[i]=(char*)malloc(MAX_ARG_LEN);
command2.argv[i]=(char*)malloc(MAX_ARG_LEN);
}
parsePath(pathv);
//main loop
while(TRUE){
printPrompt();
//read the command line and parse it
readCommand(commandLine);
if(
(strcmp(commandLine,"exit")==0)||(strcmp(commandLine,"quit")==0))break;//quit the shell
fg=parseCommand(commandLine,&command);
#ifdef DEBUG
printf("...returned from parseCommand(%d)...\n",fg);
for(i=0;i<command.argc;i++)
printf(" argv[%d]=%s\n",i, command.argv[i]);
#endif
//Scan argv for ,and|
isInRedir=isOutRedir=isPipe=0;
for(i=1;i<command.argc;i++){
if(strcmp(command.argv[i],"<")==0){
isInRedir=i;//apply to the i-th arg
for(j=i;j<command.argc;j++)
command.argv[j]=command.argv[j+1];
command.argc--;
}
if(strcmp(command.argv[i],">")==00){
isOutRedir=i;//apply to the i-th arg
for(j=i;j<command.argc;j++)
command.argv[j]=command.argv[j+1];
command.argc--;
}
if(strcmp(command.argv[i],"|")==0){
isPipe=i;//apply to the i-th arg
for(j=i;j<command.argc;j++)
command.argv[j]=command.argv[j+1];
command.argc--;
}
}
printf("isInRedir is %d,isOutRedir is %d,isPipe is %d\n",isInRedir,isOutRedir,isPipe);
//get the full pathname for the file
command.name=lookupPath(command.argv,pathv);
#ifdef DEBUG
printf("...returned from lookupPath...\n");
printf(" command path=%s\n",command.name);
for(i=0;i<command.argc;i++)
printf(" argv[%d]=%s\n",i, command.argv[i]);
#endif
if(command.name==NULL){
fprintf(stderr,"Command %s not found\n",command.argv[0]);
continue;
}
//create a process to execute command
if((chPID=fork())==0){//创建新进程
//this is the child子进程
if(isInRedir){
//Input redirection
fid=open(command.argv[isInRedir],O_RDONLY);
close(STD_INPUT);
dup(fid);
close(fid);
for(j=isInRedir;j<command.argc;j++)
command.argv[j]=command.argv[j+1];
command.argc--;
}
if(isOutRedir){
//output redirection
fid=open(command.argv[isOutRedir],O_WRONLY|O_CREAT);
close(STD_OUTPUT);
dup(fid);
close(fid);
for(j=isOutRedir;j<command.argc;j++)
command.argv[j]=command.argv[j+1];
command.argc--;
}
if(isPipe){
//aaa xxx|bbb yyy=>exec aaa&bbb
command2.argc=command.argc-isPipe;
for(j=0;j<command2.argc;j++)
command2.argv[j]=command.argv[isPipe+j];
strcpy(command2.name,command2.argv[0]);
command.argc=isPipe;
command.argv[isPipe]='\0';
printf("sub CLs formed:%s|%s\n",command.name,command2.name);
pipe(pipeID);
if(fork()==0){//创建进程
//first child does second command,redirect stdin
close(STD_INPUT);//close stdin
dup(pipeID[0]);//stdin is now read end of pipe程序的标准输入连接到管道的读出端
close(pipeID[0]);//close read end
close(pipeID[1]);//close write end
printf("exec_ing child:%s\n",command2.name);
execvp(command2.name,command2.argv);
}
if(fork()==0){//创建进程
//second child does first command,redirect stdout
close(STD_OUTPUT);//close stdout
dup(pipeID[1]);//stdout is now write end程序的标准输出连接到管道的写入端
close(pipeID[0]);//close read end
close(pipeID[1]);//close write end of pipe
printf("exec-ing child:%s\n",command.name);
execvp(command.name,command.argv);
}
//parent waits for first child,and last child below
wait(&stat);//等待子进程结束
printf("First child done\n");
}else{
#ifdef DEBUG
printf("child executing:%s\n",command.name);
for(i=1;i<command.argc;i++)
printf(" argv[%d]=%s\n",i, command.argv[i]);
#endif
execv(command.name,command.argv);}}
if(fg){//wait for the child to terminate
#ifdef DEBUG
printf("Parent waiting\n");
#endif
thisChPID=wait(&stat);
printf("Last child done\n");
}else{//put child in the background and continue
#ifdef DEBUG
printf("(...Putting command in the background)\n");
#endif}}
//shell termination}
char *lookupPath(char **argv,char **dir){
//this function inspired by one written by ZhangYuan in Summer 2009
int i;
char *result;
char pName[MAX_PATH_LEN];
//check to see if file name is already an absolute path name
if(*argv[0]=='/'){
result=(char*)malloc(strlen(argv[0]+1));
strcpy(result,argv[0]);
return result;
}
//look in path directories
//this code does not handle the case of a relative pathname
for(i=0;i<MAX_PATHS;i++){
if(dir[i]==NULL)break;
strcpy(pName,dir[i]);
strcat(pName,"/");
strcat(pName,argv[0]);
#ifdef DEBUG
printf("lookupPath:Checking for %s\n",pName);
#endif
if(access(pName,X_OK|F_OK)!=-1){
//file found
#ifdef DEBUG
printf("lookupPath:Found %s in %s(full path is %s)\n",argv[0],dir[i],pName);
#endif
result=(char*)malloc(strlen(pName)+1);
strcpy(result,pName);
return result;//return with success}}
//file name not found in any path variable
fprintf(stderr,"%s:command not found\n",argv[0]);
return NULL;}
int parseCommand(char *cLine,struct command_t *cmd){
//determine command name and construct the parameter list
int i,j;
int argc,rValue;
char *tCLine;
char **clPtr;
//initialization
tCLine=cLine;
clPtr=&tCLine;
argc=0;
rValue=TRUE;
//check for & line termination
i=strlen(cLine)-1;//last character on command line
if(cLine[i]=='&'){
printf("parseCommand:Found &\n");
cLine[i]='\0';
rValue=FALSE;}
//get the command name and parameters
//this code does not handle multiple WHITESPACE characters
while((cmd->argv[argc++]=strsep(clPtr,WHITESPACE))!=NULL);
cmd->argv[argc--]='\0';//null terminated list of strings
cmd->argc=argc;
return rValue;}
int parsePath(char *dirs[]){
//this routine based ont one written by ZhangYuan,Summer 2009
int i;
char *pathEnvVar;
register char *thePath,*oldp;
for(i=0;i<MAX_ARGS;i++)
dirs[i]=NULL;
pathEnvVar=(char*)getenv("PATH");
thePath=(char*)malloc(strlen(pathEnvVar)+1);
strcpy(thePath,pathEnvVar);
i=0;
oldp=thePath;
for(;;thePath++){
if((*thePath==':')||(*thePath=='\0')){
dirs[i]=oldp;
i++;
if(*thePath=='\0')break;
*thePath='\0';
oldp=thePath+1;}}
#ifdef DEBUG
printf("Directories in PATH variable\n");
for(i=0;i<MAX_PATHS;i++)
if(dirs[i]!='\0')
printf(" Directory[%d]:%s\n",i,dirs[i]);
#endif}
void printPrompt(){
printf("%s",promptString);}
void readCommand(char *buffer){
gets(buffer);}
#1楼 得分:0回复于:2009-06-09 11:14:51
0分作业帖,鉴定完毕。
#2楼 得分:0回复于:2009-07-24 16:00:10
myshell.h
#define FALSE 0
#define TRUE 1
#define LINE_LEN 80
#define MAX_ARGS 64
#define MAX_ARG_LEN 16
#define MAX_PATHS 64
#define MAX_PATH_LEN 96
#define WHITESPACE ".,\t&"
#define STD_INPUT 0
#define STD_OUTPUT 1
#ifndef NULL
#define NULL 0
#endif
struct command_t{
char *name;
int argc;
char *argv[MAX_ARGS];
};
myshell.c
#include
#include
#include
#include
#include
#include
#include
char *lookupPath(char **,char**);
int parseCommand(char *,struct command_t*);
int parsePath(char **);
void printPrompt();
void readCommand(char*);
char promptString[]="vivu shell>";//定义shell名为vivu
int main(){//调度整个shell执行的流程
char commandLine[LINE_LEN];
char *pathv[MAX_PATHS];
int numPaths;
int i,j,len;
int chPID;//child pid
int stat;//used by parent wait
int fg;//command in foreground?
int fid;
int pipeID[2];//创建管道
int isInRedir,isOutRedir,isPipe;
pid_t thisChPID;
struct command_t command,command2;
//shell initialization
for(i=0;i<MAX_ARGS;i++){
command.argv[i]=(char*)malloc(MAX_ARG_LEN);
command2.argv[i]=(char*)malloc(MAX_ARG_LEN);
}
parsePath(pathv);
//main loop
while(TRUE){
printPrompt();
//read the command line and parse it
readCommand(commandLine);
if(
(strcmp(commandLine,"exit")==0)||(strcmp(commandLine,"quit")==0))break;//quit the shell
fg=parseCommand(commandLine,&command);
#ifdef DEBUG
printf("...returned from parseCommand(%d)...\n",fg);
for(i=0;i<command.argc;i++)
printf(" argv[%d]=%s\n",i, command.argv[i]);
#endif
//Scan argv for ,and|
isInRedir=isOutRedir=isPipe=0;
for(i=1;i<command.argc;i++){
if(strcmp(command.argv[i],"<")==0){
isInRedir=i;//apply to the i-th arg
for(j=i;j<command.argc;j++)
command.argv[j]=command.argv[j+1];
command.argc--;
}
if(strcmp(command.argv[i],">")==00){
isOutRedir=i;//apply to the i-th arg
for(j=i;j<command.argc;j++)
command.argv[j]=command.argv[j+1];
command.argc--;
}
if(strcmp(command.argv[i],"|")==0){
isPipe=i;//apply to the i-th arg
for(j=i;j<command.argc;j++)
command.argv[j]=command.argv[j+1];
command.argc--;
}
}
printf("isInRedir is %d,isOutRedir is %d,isPipe is %d\n",isInRedir,isOutRedir,isPipe);
//get the full pathname for the file
command.name=lookupPath(command.argv,pathv);
#ifdef DEBUG
printf("...returned from lookupPath...\n");
printf(" command path=%s\n",command.name);
for(i=0;i<command.argc;i++)
printf(" argv[%d]=%s\n",i, command.argv[i]);
#endif
if(command.name==NULL){
fprintf(stderr,"Command %s not found\n",command.argv[0]);
continue;
}
//create a process to execute command
if((chPID=fork())==0){//创建新进程
//this is the child子进程
if(isInRedir){
//Input redirection
fid=open(command.argv[isInRedir],O_RDONLY);
close(STD_INPUT);
dup(fid);
close(fid);
for(j=isInRedir;j<command.argc;j++)
command.argv[j]=command.argv[j+1];
command.argc--;
}
if(isOutRedir){
//output redirection
fid=open(command.argv[isOutRedir],O_WRONLY|O_CREAT);
close(STD_OUTPUT);
dup(fid);
close(fid);
for(j=isOutRedir;j<command.argc;j++)
command.argv[j]=command.argv[j+1];
command.argc--;
}
if(isPipe){
//aaa xxx|bbb yyy=>exec aaa&bbb
command2.argc=command.argc-isPipe;
for(j=0;j<command2.argc;j++)
command2.argv[j]=command.argv[isPipe+j];
strcpy(command2.name,command2.argv[0]);
command.argc=isPipe;
command.argv[isPipe]='\0';
printf("sub CLs formed:%s|%s\n",command.name,command2.name);
pipe(pipeID);
if(fork()==0){//创建进程
//first child does second command,redirect stdin
close(STD_INPUT);//close stdin
dup(pipeID[0]);//stdin is now read end of pipe程序的标准输入连接到管道的读出端
close(pipeID[0]);//close read end
close(pipeID[1]);//close write end
printf("exec_ing child:%s\n",command2.name);
execvp(command2.name,command2.argv);
}
if(fork()==0){//创建进程
//second child does first command,redirect stdout
close(STD_OUTPUT);//close stdout
dup(pipeID[1]);//stdout is now write end程序的标准输出连接到管道的写入端
close(pipeID[0]);//close read end
close(pipeID[1]);//close write end of pipe
printf("exec-ing child:%s\n",command.name);
execvp(command.name,command.argv);
}
//parent waits for first child,and last child below
wait(&stat);//等待子进程结束
printf("First child done\n");
}else{
#ifdef DEBUG
printf("child executing:%s\n",command.name);
for(i=1;i<command.argc;i++)
printf(" argv[%d]=%s\n",i, command.argv[i]);
#endif
execv(command.name,command.argv);}}
if(fg){//wait for the child to terminate
#ifdef DEBUG
printf("Parent waiting\n");
#endif
thisChPID=wait(&stat);
printf("Last child done\n");
}else{//put child in the background and continue
#ifdef DEBUG
printf("(...Putting command in the background)\n");
#endif}}
//shell termination}
char *lookupPath(char **argv,char **dir){
//this function inspired by one written by ZhangYuan in Summer 2009
int i;
char *result;
char pName[MAX_PATH_LEN];
//check to see if file name is already an absolute path name
if(*argv[0]=='/'){
result=(char*)malloc(strlen(argv[0]+1));
strcpy(result,argv[0]);
return result;
}
//look in path directories
//this code does not handle the case of a relative pathname
for(i=0;i<MAX_PATHS;i++){
if(dir[i]==NULL)break;
strcpy(pName,dir[i]);
strcat(pName,"/");
strcat(pName,argv[0]);
#ifdef DEBUG
printf("lookupPath:Checking for %s\n",pName);
#endif
if(access(pName,X_OK|F_OK)!=-1){
//file found
#ifdef DEBUG
printf("lookupPath:Found %s in %s(full path is %s)\n",argv[0],dir[i],pName);
#endif
result=(char*)malloc(strlen(pName)+1);
strcpy(result,pName);
return result;//return with success}}
//file name not found in any path variable
fprintf(stderr,"%s:command not found\n",argv[0]);
return NULL;}
int parseCommand(char *cLine,struct command_t *cmd){
//determine command name and construct the parameter list
int i,j;
int argc,rValue;
char *tCLine;
char **clPtr;
//initialization
tCLine=cLine;
clPtr=&tCLine;
argc=0;
rValue=TRUE;
//check for & line termination
i=strlen(cLine)-1;//last character on command line
if(cLine[i]=='&'){
printf("parseCommand:Found &\n");
cLine[i]='\0';
rValue=FALSE;}
//get the command name and parameters
//this code does not handle multiple WHITESPACE characters
while((cmd->argv[argc++]=strsep(clPtr,WHITESPACE))!=NULL);
cmd->argv[argc--]='\0';//null terminated list of strings
cmd->argc=argc;
return rValue;}
int parsePath(char *dirs[]){
//this routine based ont one written by ZhangYuan,Summer 2009
int i;
char *pathEnvVar;
register char *thePath,*oldp;
for(i=0;i<MAX_ARGS;i++)
dirs[i]=NULL;
pathEnvVar=(char*)getenv("PATH");
thePath=(char*)malloc(strlen(pathEnvVar)+1);
strcpy(thePath,pathEnvVar);
i=0;
oldp=thePath;
for(;;thePath++){
if((*thePath==':')||(*thePath=='\0')){
dirs[i]=oldp;
i++;
if(*thePath=='\0')break;
*thePath='\0';
oldp=thePath+1;}}
#ifdef DEBUG
printf("Directories in PATH variable\n");
for(i=0;i<MAX_PATHS;i++)
if(dirs[i]!='\0')
printf(" Directory[%d]:%s\n",i,dirs[i]);
#endif}
void printPrompt(){
printf("%s",promptString);}
void readCommand(char *buffer){
gets(buffer);}
#1楼 得分:0回复于:2009-06-09 11:14:51
0分作业帖,鉴定完毕。
#2楼 得分:0回复于:2009-07-24 16:00:10
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
不是函数
追问
比如 /etc/rc.d/rc中的
# First, run the KILL scripts.
for i in /etc/rc$runlevel.d/K* ; do
check_runlevel "$i" || continue
# Check if the subsystem is already up.
subsys=${i#/etc/rc$runlevel.d/K??}
[ -f /var/lock/subsys/$subsys -o -f /var/lock/subsys/$subsys.init ] \
|| continue
done
里面就好像是把$当作函数来用的
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
hello.c 你的shell和hello.c在同一个目录
要不就加上路径
./a.out
要不就加上路径
./a.out
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询