command相关知识
-
Linux command linePrefaceI would be fooling myself if I claim to be proficient in Linux command line. So I took the Linux Command Line video course to enhance my knowledge in the area.ls: list directory contentUserful Options:OptionMeaning-luse a long listing format-ado not ignore entries starting with .-hwith -l, print sizes in human readable format (e.g. 1K 234M 2G)whatis: displays short manual page descriptionswhatis cp // output: cp(1) - copy f
-
Perl one-line command命令行调用perl [ -sTtuUWX ][ -hv ] [ -V[:configvar] ][ -cw ] [ -d[t][:debugger] ] [ -D[number/list] ][ -pna ] [ -Fpattern ] [ -l[octal] ] [ -0[octal/hexadecimal] ][ -Idir ] [ -m[-]module ] [ -M[-]'module...' ] [ -f ][ -C [number/list] ][ -P ][ -S ][ -x[dir] ][ -i[extension] ][ [-e|-E] 'command' ] [ -- ] [ programfile ] [ argument ]...
-
设计模式之美:Command(命令)索引别名意图结构参与者适用性效果相关模式实现实现方式(一):直接注入 Receiver 对象,Command 决定调用哪个方法。实现方式(二):注入 Receiver 的指定方法,Command 仅能调用该方法。实现方式(三):参数化 Command 构造。实现方式(四):使用泛型减少 Command 子类。实现方式(五):使用弱引用代替对 Receiver 的强引用。实现方式(六):使 Command 支持 Undo 和 Redo。实现方式(七):使 MacroCommand 来管理 Command 序列。别名ActionTransaction意图将一个请求封装为一个对象,从而使你可用不同的请求对客户进行参数化;对请求排队或记录请求日志,以及支持可撤销的操作。Encapsulate a request as an object, thereby letting you parameterize clients with different requests, queue or log requests,
-
Command 模式 Step by StepCommand 模式 Step by step引言提起Command模式,我想没有什么比遥控器的例子更能说明问题了,本文将通过它来一步步实现GOF的Command模式。我们先看下这个遥控器程序的需求:假如我们需要为家里的电器设计一个远程遥控器,通过这个控制器,我们可以控制电器(诸如灯、风扇、空调等)的开关。我们的控制器上有一系列的按钮,分别对应家中的某个电器,当我们在遥控器上按下“On”时,电器打开;当我们按下“Off”时,电器关闭。好了,让我们开始Command 模式之旅吧。HardCoding的实现方式控制器的实现一般来说,考虑问题通常有两种方式:从最复杂的情况考虑,也就是尽可能的深谋远虑,设计之初就考虑到程序的可维护性、扩展性;还有就是从最简单的情况考虑,不考虑扩展,客户要求什么,我们就做个什么,至于以后有什么新的需求,等以后再说。当然这两种方式各有优劣,本文我们从最简单的情况开始考虑。我们假设控制器只能控制 三个电器,分别是:灯、电扇、门(你就当是电子门好了^^)。那么我们的控制器应该有三组,共六个
command相关课程
command相关教程
- 2.6 command 例如上面的配置里,我们希望修改 Redis 的启动命令,加入配置文件以便对 Redis 服务进行配置,那么我们可以直接通过 command 配置来修改。command会覆盖镜像中的CMD指令。
- 2.1 Scrapy command 的执行过程 首先查看 Scrapy 命令的代码,其内容如下:# 查看scrapy的命令文件位置[root@server2 ~]# which scrapy/usr/local/bin/scrapy# 查看命令文件内的代码[root@server2 ~]# cat /usr/local/bin/scrapy #!/usr/bin/python3# -*- coding: utf-8 -*-import reimport sysfrom scrapy.cmdline import executeif __name__ == '__main__': sys.argv[0] = re.sub(r'(-script\.pyw?|\.exe)?$', '', sys.argv[0]) sys.exit(execute())是不是很简单?不过深入下去就复杂了。我们来看这个最关键的 execute()方法:# 源码位置:scrapy/cmdline.py# ...def execute(argv=None, settings=None): if argv is None: argv = sys.argv # 设置配置参数 if settings is None: settings = get_project_settings() # set EDITOR from environment if available try: editor = os.environ['EDITOR'] except KeyError: pass else: settings['EDITOR'] = editor inproject = inside_project() # 这里是导入所有scrapy支持的命令 cmds = _get_commands_dict(settings, inproject) # 这里是要执行的子命令名,比如startproject、crawl等 cmdname = _pop_command_name(argv) # 可以额外加上一行打印语句 # print("inproject={}, cmds={}, cmdname={}".format(inproject, cmds, cmdname)) # 解析命令行参数选项 parser = optparse.OptionParser(formatter=optparse.TitledHelpFormatter(), conflict_handler='resolve') if not cmdname: '''没有带子命令,打印帮助命令后退出''' _print_commands(settings, inproject) sys.exit(0) elif cmdname not in cmds: '''未知子命令,打印错误信息后退出''' _print_unknown_command(settings, cmdname, inproject) sys.exit(2) # 取出这个对应命令文件的Command类,其需要区分是否在爬虫项目中 cmd = cmds[cmdname] parser.usage = "scrapy %s %s" % (cmdname, cmd.syntax()) parser.description = cmd.long_desc() settings.setdict(cmd.default_settings, priority='command') # 设置相关 cmd.settings = settings cmd.add_options(parser) # 解析子命令带的参数 opts, args = parser.parse_args(args=argv[1:]) # 处理子命令参数 _run_print_help(parser, cmd.process_options, args, opts) # 核心,设置command类的核心处理类 cmd.crawler_process = CrawlerProcess(settings) # 会调用command类的run()方法执行 _run_print_help(parser, _run_command, cmd, args, opts) sys.exit(cmd.exitcode)这个函数是所有 scrapy 命令要执行的函数,函数中得到的 cmds 是 scrapy/command 下所有或部分 Command 类集合,也即表示 scrapy 所支持的命令操作。我们可以手动在其后面打印一条语句 (上面注释的 print() 方法),来看看 cmds 的具体值:# 这个时候新建scrapy项目,inproject=False[root@server2 shen]# scrapy startproject qidian_spiderinproject=False, cmds={'commands': <scrapy.commands.BaseRunSpiderCommand object at 0x7ff3b3231470>, 'bench': <scrapy.commands.bench.Command object at 0x7ff3b3223be0>, 'fetch': <scrapy.commands.fetch.Command object at 0x7ff3b32cb080>, 'genspider': <scrapy.commands.genspider.Command object at 0x7ff3b32c6b00>, 'runspider': <scrapy.commands.runspider.Command object at 0x7ff3b32c6e48>, 'settings': <scrapy.commands.settings.Command object at 0x7ff3b32c6ac8>, 'shell': <scrapy.commands.shell.Command object at 0x7ff3b32c6a90>, 'startproject': <scrapy.commands.startproject.Command object at 0x7ff3b32c69e8>, 'version': <scrapy.commands.version.Command object at 0x7ff3b32c6940>, 'view': <scrapy.commands.view.Command object at 0x7ff3b32c6978>}, cmdname=startprojectNew Scrapy project 'qidian_spider', using template directory '/usr/local/lib64/python3.6/site-packages/scrapy/templates/project', created in: /root/shen/qidian_spiderYou can start your first spider with: cd qidian_spider scrapy genspider example example.com注意到这是在没有 scrapy 项目时执行的,我们在创建了上述项目之后,进入 qidian_spider 爬虫项目目录中,在继续执行:# 进入到scrapy项目内,此时在运行scrapy命令时,inproject=True[root@server2 qidian_spider]# scrapy listinproject=True, cmds={'commands': <scrapy.commands.BaseRunSpiderCommand object at 0x7fed7be984e0>, 'bench': <scrapy.commands.bench.Command object at 0x7fed7be8bc50>, 'check': <scrapy.commands.check.Command object at 0x7fed7bf32128>, 'crawl': <scrapy.commands.crawl.Command object at 0x7fed7bf2cba8>, 'edit': <scrapy.commands.edit.Command object at 0x7fed7bf2cb70>, 'fetch': <scrapy.commands.fetch.Command object at 0x7fed7bf2c860>, 'genspider': <scrapy.commands.genspider.Command object at 0x7fed7bf2c8d0>, 'list': <scrapy.commands.list.Command object at 0x7fed7bf2cb00>, 'parse': <scrapy.commands.parse.Command object at 0x7fed7bf2cac8>, 'runspider': <scrapy.commands.runspider.Command object at 0x7fed7beae908>, 'settings': <scrapy.commands.settings.Command object at 0x7fed7beaea58>, 'shell': <scrapy.commands.shell.Command object at 0x7fed7beaeac8>, 'startproject': <scrapy.commands.startproject.Command object at 0x7fed7beaeba8>, 'version': <scrapy.commands.version.Command object at 0x7fed7beaebe0>, 'view': <scrapy.commands.view.Command object at 0x7fed7beaec50>}, cmdname=list有点理解了吧?在爬虫项目之外的,支持10个命令;创建了爬虫项目后,进入到项目中后,则支持前面所有的命令。这里 inproject 值就是判断执行的 scrapy 命令是否在项目内的。命令文件内的Command类在·commands 目录下的 __init__.py 文件中定义了 ScrapyCommand 基类,然后所有命令文件中都会定义 Command 类,用于描述对应命令。该类统一都继承自 ScrapyCommand 类,且有一个 requires_project 属性来辅助判断该命令是否依赖 project。这个属性正是控制下面语句执行的结果:cmds = _get_commands_dict(settings, inproject)关联的函数如下:# 源码位置:scrapy/cmdline.py# ...def _get_commands_from_module(module, inproject): d = {} for cmd in _iter_command_classes(module): # 这里inproject为True表示在项目内,此时if条件全部满足; # inproject为False时,只有命令文件中Command类的requires_project属性为False时才执行if语句 if inproject or not cmd.requires_project: cmdname = cmd.__module__.split('.')[-1] d[cmdname] = cmd() return d# ...def _get_commands_dict(settings, inproject): # 看上面的函数,如何得到cmds? cmds = _get_commands_from_module('scrapy.commands', inproject) cmds.update(_get_commands_from_entry_points(inproject)) cmds_module = settings['COMMANDS_MODULE'] if cmds_module: cmds.update(_get_commands_from_module(cmds_module, inproject)) return cmds# ...在回到前面的 execute() 方法中,由我们前面给出的注释来看,最核心的代码就是如下两句:# ...def execute(argv=None, settings=None): # ... # 核心,设置command类的核心处理类 cmd.crawler_process = CrawlerProcess(settings) # 会调用command类的run()方法执行 _run_print_help(parser, _run_command, cmd, args, opts) # ...我们知道 cmd 表示的正是对应命令文件下的 Command 类实例,它的一个很重要的属性就是 crawler_process, 后面我们也会介绍到它。接下来的执行代码正是第二句,我们继续跟踪调用其方法:# 源码位置:scrapy/cmdline.py# ...def _run_print_help(parser, func, *a, **kw): try: func(*a, **kw) except UsageError as e: if str(e): parser.error(str(e)) if e.print_help: parser.print_help() sys.exit(2)看到没,前面的语句等价于执行 _run_command(cmd, args, opts),继续跟踪 _run_command() 方法:# 源码位置:scrapy/cmdline.py# ...def _run_command(cmd, args, opts): if opts.profile: _run_command_profiled(cmd, args, opts) else: # 通常情况下执行这里的run()方法 cmd.run(args, opts)一般情况代码是执行下面的 cmd.run() 方法。至此,答案已经呼之欲出了。Scrapy 命令的执行就是调用对应命令文件下的 Command 类中的 run() 方法。
- 4.3 command: 具体对指定的文件进行怎样的处理,例如对模式空间内的内容进行增删改查具体的操作。4.3.1 增i:insert,在制定或匹配到的行前面添加新行内容为 string,i\string,为 /etc/passwd 中的第一行前面添加一行内容为 "####",例如:[root@shell workspace]# sed '1i####' /etc/passwd####root:x:0:0:root:/root:/bin/bashbin:x:1:1:bin:/bin:/sbin/nologindaemon:x:2:2:daemon:/sbin:/sbin/nologin.....a:append,在指定或匹配到的行后面追加新行,内容为 string,a\string。为 /etc/passwd 的第一行后面添加内容 "aaa",例如:[root@shell workspace]# sed '1a###' /etc/passwdroot:x:0:0:root:/root:/bin/bash###bin:x:1:1:bin:/bin:/sbin/nologindaemon:x:2:2:daemon:/sbin:/sbin/nologinadm:x:3:4:adm:/var/adm:/sbin/nologin4.3.2 删d:delete,删除符合地址定界条件的的行删除 /etc/inittab 文件中注释行,例如:sed '/^#/d' /etc/inittab直接利用元字符匹配铆定以#开头的行进行删除操作,注意此处没有直接修改文件,如果添加 -i 选项,则直接对源文件进行修改,一般需要先备份,然后操作以免误操作原始文件。4.3.3 改s:s/pattern/string/修饰符: 查找并替换,默认只替换每行中第一次被模式匹配到的字符串 ,如果修饰符为 g, 则为全部替换。替换 /etc/passwd 中的 root 为大写,例如:[root@shell workspace]# sed 's/root/ROOT/g' /etc/passwdROOT:x:0:0:ROOT:/ROOT:/bin/bashbin:x:1:1:bin:/bin:/sbin/nologindaemon:x:2:2:daemon:/sbin:/sbin/nologin...替换 /etc/inittab 文件中 "id:3:initdefault:" 一行中的数字为 5,例如:sed 's/id:[0-9]/id:5/g' /etc/inittab在此利用元字符匹配 [0-9] 的一个数字进行替换为 id:5。4.3.4 查p:print,默认 sed 对模式空间内的处理完毕后,将输出的结果输出在标准输出,添加 p 命令,相当于输出了原文,又一次输出了模式匹配处理后的内容。打印 /etc/passwd 的第三行,指定行号,例如:[root@shell workspace]# sed -n '3p' /etc/passwddaemon:x:2:2:daemon:/sbin:/sbin/nologin在此直接指定地址定界 3,然后 command 为 p 打印输出,由于不想输出 /etc/passwd 的全部内容,因此添加了 - n 选项。
- 8. 实现 cat 命令 编写文件 cat.py,定义类 CatCommand 用于实现 cat 命令的功能:from command import Commandclass CatCommand(Command): def __init__(self, args): Command.__init__(self, args) def usage(self): print('cat file - print file')在第 1 行,从 command 模块中导入类 Command在第 3 行,定义类 HelpCommand,继承于类 Command在第 5 行,调用父类的构造函数在第 7 行,定义方法 usage 打印 cat 命令的功能 def execute(self): if len(self.args) != 2: self.usage() return path = self.args[1]如果命令为 cat file,则设置 path 为 args[1] file = open(path) for line in file: print(line, end = '') file.close() 使用 for 循环遍历文件的每一行,并输出
- 10. 实现 rm 命令 编写文件 rm.py,定义类 RmCommand 用于实现 rm 命令的功能from command import Commandimport osclass RmCommand(Command): def __init__(self, args): Command.__init__(self, args) def usage(self): print('rm file - remove file')在第 1 行,从 command 模块中导入类 Command在第 3 行,定义类 RmCommand,继承于类 Command在第 5 行,调用父类的构造函数在第 7 行,定义方法 usage 打印 rm 命令的功能 def execute(self): if len(self.args) != 2: self.usage() return path = self.args[1] os.remove(path)调用 os 模块的 remove 方法删除文件
- 3.1 语法 avdmanager [global options] command [command options]
command相关搜索
-
c 正则表达式
c string
c 编程
c 程序设计
c 程序设计教程
c 多线程编程
c 教程
c 数组
c 委托
c 下载
c 线程
c 语言
caidan
cakephp
call
calloc
calu
camera
caption
case语句