我想阅读linux命令编写的实际源代码.我已经获得了使用它们的一些经验,现在我认为是时候在更深层次上与我的机器进行交互了.
我在http://directory.fsf.org/wiki/GNU找到了一些命令.不幸的是,我无法找到像'ls'这样的基本命令,这在我看来很容易开始.
我究竟如何阅读像'ls'这样的简单shell命令的源代码?我在Ubuntu 12.04上运行
以下代码有什么问题?
name='$filename | cut -f1 -d'.''
Run Code Online (Sandbox Code Playgroud)
因为,我得到文字字符串$filename | cut -f1 -d'.',但如果我删除引号,我什么都没得到.同时打字
"test.exe" | cut -f1 -d'.'
Run Code Online (Sandbox Code Playgroud)
在shell中给我输出我想要的输出test.我已经知道$filename已经分配了正确的价值.我想要做的是为变量分配没有扩展名的文件名.
例如,给定:
USCAGoleta9311734.5021-120.1287855805
Run Code Online (Sandbox Code Playgroud)
我想提取:
US
Run Code Online (Sandbox Code Playgroud) 不同长度的2个未排序文本文件如何可以是由侧显示侧(在列)在一个shell
给定one.txt和two.txt:
$ cat one.txt
apple
pear
longer line than the last two
last line
$ cat two.txt
The quick brown fox..
foo
bar
linux
skipped a line
Run Code Online (Sandbox Code Playgroud)
显示:
apple The quick brown fox..
pear foo
longer line than the last two bar
last line linux
skipped a line
Run Code Online (Sandbox Code Playgroud)
paste one.txt two.txt几乎没有诀窍,但没有很好地对齐列,因为它只是在第1列和第2列之间打印一个选项卡.我知道如何使用emacs和vim,但希望输出显示为stdout用于管道等.
我提出的解决方案使用sdiff然后管道sed删除输出sdiff添加.
sdiff one.txt two.txt | sed -r 's/[<>|]//;s/(\t){3}//'
我可以创建一个函数并将其粘贴在我的.bashrc但是肯定已经存在的命令(或者可能是更清洁的解决方案)?
我想做这样的事情:
cat abcd.txt | cut -f 2,1
Run Code Online (Sandbox Code Playgroud)
我希望输出中的顺序为2然后为1.在我正在测试的机器上(FreeBSD 6),这没有发生(它以1,2顺序打印).你能告诉我怎么做吗?
我知道我总是可以编写一个shell脚本来执行此操作,但我正在寻找使用'cut'命令选项的东西.
我想我正在使用包含cut的coreutils的5.2.1版本.
任何人都可以提供指向linux命令源代码的指针,如'sort','uniq'等?
我正在阅读coreutils源代码以学习linux下的编程.
我发现在大多数程序中,例如ls.c,cat.c,它们在前几行调用宏函数initialize_main().所以我查看了system.h以找到实现:
/* Redirection and wildcarding when done by the utility itself.
Generally a noop, but used in particular for native VMS. */
#ifndef initialize_main
# define initialize_main(ac, av)
#endif
Run Code Online (Sandbox Code Playgroud)
我不明白为什么定义这样的宏,我不明白这个评论.
我正在看一下'less'实用程序的代码,特别是它如何获得键盘输入.有趣的是,在ttyin.c的第80行,它将文件描述符设置为:
/*
* Try /dev/tty.
* If that doesn't work, use file descriptor 2,
* which in Unix is usually attached to the screen,
* but also usually lets you read from the keyboard.
*/
#if OS2
/* The __open() system call translates "/dev/tty" to "con". */
tty = __open("/dev/tty", OPEN_READ);
#else
tty = open("/dev/tty", OPEN_READ);
#endif
if (tty < 0)
tty = 2;
Run Code Online (Sandbox Code Playgroud)
是不是文件描述符2 stderr?如果是这样,WTH ?! 我以为键盘输入是通过stdin发送的.
有趣的是,即使你这样做ls -l * | less,在文件加载完成后,你仍然可以使用键盘上下滚动,但如果你这样做ls -l * | vi …
我猜这个网站上的大多数人都熟悉尾部,如果没有 - 它提供了一种"跟随"模式,当文本附加到文件尾部时会将这些字符转储到终端.
我正在寻找(并可能在必要时写自己)是一个适用于二进制文件的尾部版本.基本上我有一个无线链接,我想通过另一个网络链接来涓流文件.查看尾部源代码,重写起来并不难,但我宁愿不重新发明轮子!这不是严格意义上的"尾部",因为我希望整个文件被复制,但它会在新的字节被添加并流式传输时观察.
想法?
我正在处理(希望)UTF-8文本的大文件.我可以使用Ubuntu 13.10(3.11.0-14-generic)和12.04重现它.
在调查一个bug我遇到了奇怪的行为
$ export LC_ALL=en_US.UTF-8
$ sort part-r-00000 | uniq -d
? ? ? ? 251
? ? ? ? ? 291
? ? ? ? 301
? ? 475
? ? 565
$ export LC_ALL=C
$ sort part-r-00000 | uniq -d
$ # no duplicates found
Run Code Online (Sandbox Code Playgroud)
运行使用读取文件的自定义C++程序时也会出现重复项
C++似乎至少对于std::stringstream- 由于使用en_US.UTF-8区域设置时出现重复,它会失败.std::string输入/输出没有影响.
为什么在使用UTF-8语言环境时找到重复项,并且在C语言环境中找不到重复项?
导致此行为的文本的区域设置有哪些转换?
编辑:这是一个小例子
$ uniq -D duplicates.small.nfc
? ? ? ? ? 224
? ? ? ? ? 224
? ? …Run Code Online (Sandbox Code Playgroud)