Articles of shell

在Linux和BSD中使用和不使用shebang的Bash脚本执行

如何和谁决定什么时候执行一个Bash类的脚本作为一个二进制文件执行没有shebang? 我想用 shebang运行一个正常的脚本是用binfmt_script Linux模块来处理的,它会检查一个shebang,parsing命令行并运行指定的脚本解释器。 但是如果有人在不使用shebang的情况下运行脚本,会发生什么? 我已经testing了直接执行execv方法,发现那里没有内核魔法 – 即像这样的文件: $ cat target-script echo Hello echo "bash: $BASH_VERSION" echo "zsh: $ZSH_VERSION" 运行只执行execv调用的编译的C程序会产生: $ cat test-runner.c void main(){ if(execv(“./ target-script”,0)== -1) PERROR(); } $ ./test-runner ./target-script:执行格式错误 但是,如果我从另一个shell脚本执行相同的操作,它将使用与原始shell解释器相同的shell解释器运行目标脚本: $ cat test-runner.bash #!/斌/庆典 ./target-script $ ./test-runner.bash 你好 bash:4.1.0(1) – 释放 zsh的: 如果我对其他shell执行相同的技巧(例如,Debian的默认sh – /bin/dash ),它也可以工作: $ cat test-runner.dash #!/斌/破折号 ./target-script […]

在Ubuntu启动时运行Python脚本

我有一个简短的Python脚本,需要在启动时运行 – Ubuntu 13.10。 我已经尝试了所有我能想到的东西,但不能让它运行。 剧本: #!/usr/bin/python import time with open("/home/username/Desktop/startup.txt", 'a') as f: f.write(str(time.time()) + " It worked!") (实际的脚本有点不同,因为我只是用它来进行testing,但你明白了。) 我已经尝试了以下所有,没有运气: 把命令python startuptest.py放在crontab ,作为@reboot python /home/username/Documents/startuptest.py ,作为普通用户和sudo 将命令python /home/username/Documents/startuptest.py放入/etc/rc.local 打开Ubuntu的启动应用程序并把命令放在那里 完成上述所有操作,将命令放入shell脚本中,然后调用该shell脚本 没有任何工作 我感觉我错过了一些简单的东西。 有任何想法吗? (如果我只是从terminal运行命令,脚本运行良好。)

replacesed中的控制字符

我需要在linux的文本文件中replace所有出现的控制字符CTRL + A(SOH / ascii 1),这怎么能在SED中实现呢?

将命令输出存储到bash中的variables时,如何保留换行符?

我在Linux上使用bash shell。 我有这个简单的脚本… #!/bin/bash TEMP=`sed -n '/'"Starting deployment of"'/,/'"Failed to start context"'/p' "/usr/java/jboss/standalone/log/server.log" | tac | awk '/'"Starting deployment of"'/ {print;exit} 1' | tac` echo $TEMP 但是,当我运行这个脚本 ./temp.sh 所有的输出打印没有回车/新行。 不知道是否将输出存储到$ TEMP或echo命令本身。 如何将命令的输出存储到variables并保留换行符/回车符?

`sh`和`source`有什么区别?

sh和source什么区别? source: source filename [arguments] Read and execute commands from FILENAME and return. The pathnames in $PATH are used to find the directory containing FILENAME. If any ARGUMENTS are supplied, they become the positional parameters when FILENAME is executed. 对于man sh : NAME bash – GNU Bourne-Again SHell SYNOPSIS bash [options] [file] COPYRIGHT Bash is […]

shell脚本发送邮件

我在Linux机器上,并监视进程的使用情况。 大多数情况下,我将远离我的系统,我可以在我的设备上访问互联网。 所以我打算编写一个shell脚本,可以把这个过程的输出邮寄给我。 可能吗? 如果是的话如何让shell脚本给我发邮件? 请提供一个片段来开始。

如何打印第三列到最后一列?

我试图从DbgView日志文件中删除前两列(我不感兴趣)。 我似乎无法find一个例子,从第3列开始直到行尾。 请注意,每行都有可变的列数。

通过adb shell和代码了解命令 – Android

我正尝试将BATTERSTATS权限授予应用程序。 当我从PC运行命令 – 它工作得很好: adb shell pm grant com.example.sample.myapplication android.permission.BATTERY_STATS 但同样的pm grant命令从Android应用程序运行时不起作用: java.lang.Process process = Runtime.getRuntime().exec("pm grant com.example.sample.myapplication android.permission.BATTERY_STATS"); 是否需要root权限才能授予此权限? 如果是这样的话,为什么不需要设备通过adb shell来运行它? 我是Android新手,请更清楚地解释一下内部发生了什么以及如何继续。

来自shell的GROUP BY / SUM

我有一个包含这样的数据的大文件: a 23 b 8 a 22 b 1 我想能够得到这个: a 45 b 9 我可以先对这个文件进行sorting,然后通过扫描文件来完成。 什么是一个好的直接的命令行方式呢?

用system()执行shell脚本返回256.这是什么意思?

我写了一个shell脚本来软重启HAProxy(反向代理)。 从shell中执行脚本。 但我想要一个守护进程来执行脚本。 这是行不通的。 system()返回256.我不知道这可能意味着什么。 #!/bin/sh # save previous state mv /home/haproxy/haproxy.cfg /home/haproxy/haproxy.cfg.old mv /var/run/haproxy.pid /var/run/haproxy.pid.old cp /tmp/haproxy.cfg.new /home/haproxy/haproxy.cfg kill -TTOU $(cat /var/run/haproxy.pid.old) if haproxy -p /var/run/haproxy.pid -f /home/haproxy/haproxy.cfg; then kill -USR1 $(cat /var/run/haproxy.pid.old) rm -f /var/run/haproxy.pid.old exit 1 else kill -TTIN $(cat /var/run/haproxy.pid.old) rm -f /var/run/haproxy.pid mv /var/run/haproxy.pid.old /var/run/haproxy.pid mv /home/haproxy/haproxy.cfg /home/haproxy/haproxy.cfg.err mv /home/haproxy/haproxy.cfg.old […]