linux命令传入参数 linux命令行传参数

linux命令 怎么实现使用参数#!/bin/bash
echo "This is script show the param use:"
echo "This is the script name: $0"
echo "This is the firstparam is: $1"
echo "This is the secondparam is: $2"
echo "This is the thirdparam is: $3"
echo "This is the fourthparam is: $4"
echo "This is the fifthparam is: $5"
echo "This is the sixthparam is: $6"
echo "This is the seventhparam is: $7"
echo "This is the eighthparam is: $8"
echo "This is the ninithparam is: $9"
echo "This totalparam num is: $#"
echo "This totalparam is: $*"
使用的时候直接把你要参数加到脚本后面例如下面:
[kinyou_xy@localhost shell]$ sh param.sh one two thr good night wubi shell study last
This is script show the param use:
This is the script name: param.sh
This is the firstparam is: one
This is the secondparam is: two
This is the thirdparam is: thr
This is the fourthparam is: good
This is the fifthparam is: night
This is the sixthparam is: wubi
This is the seventhparam is: shell
This is the eighthparam is: study
This is the ninithparam is: last
This totalparam num is: 9
This totalparam is: one two thr good night wubi shell study last
如何使用Linux sed 命令将Shell的参数传进去写在文件里re.sh:
#只需要在脚本中写这一行就可以了 -i可以修改原文件
sed-i 's/define VER0/define VER$1/' cc.c
求 linux下 c++高手,命令行传参数的题 , GNU/Linux的命令行选项有两种类型linux命令传入参数:短选项和长选项linux命令传入参数,前者以 '-' 作为前导符linux命令传入参数,后者以 '--' 作为前导符
。比如有一个命令:
$ myprog -a vv --add -b --file a.txt b.txt - -- -e c.txt
在GNU/Linux系统linux命令传入参数,对这种情况的一种合理解释是:
a是短选项,带一个参数vvlinux命令传入参数;
add是长选项,无参数;
b是短选项 , 无参数;
file是长选项,带一个参数a.txt;
b.txt是参数;
-是参数 , 通常表示标准输入,stdin;
--是一个指示符 , 表明停止扫描参数,其后所有部分都是参数,而不是选项;
-e是参数;
c.txt是参数
为了简化程序设计,有几个库函数可以优雅地分析命令行参数 , 原型如下:
#include unistd.h
int getopt(int argc, char * const argv[],
const char *optstring);
extern char *optarg;
extern int optind, opterr, optopt;
#define _GNU_SOURCE
#include getopt.h
int getopt_long(int argc, char * const argv[],
const char *optstring,
const struct option *longopts, int *longindex);
int getopt_long_only(int argc, char * const argv[],
const char *optstring,
const struct option *longopts, int *longindex);
我们先看看用于分析短参数的getopt函数 。参数含义如下:
argc, argv是从main函数获取的参数,原样传给getopt;
optstring指示如何分析参数 。
关于optstring,还有几点说明:
如果选项带参数 , 该选项后接冒号 , 比如上例中optstring为"a:b" , 指示a带参数,b没有参数;
如果选项带可选参数 , 该选项后接两个冒号,比如"a::b",表明a可能有参数,也可能没有;
如果optstring的开头字符为':',表明如果指明选项带参数,而实际命令行没有参数时,getopt返回':'而不是'?'(默认情况下返回'?',和无法识别的参数返回一样);
如果optstring的开头字符为'+',表明一但遇到一个无选项参数,马上停止扫描,随后的部分当作参数来解释;
如果optstring的开头字符为'-' , 表明如果遇到无选项参数,则把它当作选项1(不是字符'1')的参数

推荐阅读