小编MOH*_*MED的帖子

一个如何在bash中使用getopts的例子

我想以myscript这种方式调用文件:

$ ./myscript -s 45 -p any_string
Run Code Online (Sandbox Code Playgroud)

要么

$ ./myscript -h  #should display help
$ ./myscript     #should display help
Run Code Online (Sandbox Code Playgroud)

我的要求是:

  • getopt 这里得到输入参数
  • 检查是否-s存在,如果没有返回错误
  • 检查之后的值-s是45还是90
  • 检查是否-p存在并且之后有输入字符串
  • 如果用户输入./myscript -h或只是./myscript显示帮助

我到目前为止尝试了这段代码:

#!/bin/bash
while getopts "h:s:" arg; do
  case $arg in
    h)
      echo "usage" 
      ;;
    s)
      strength=$OPTARG
      echo $strength
      ;;
  esac
done
Run Code Online (Sandbox Code Playgroud)

但是使用该代码我会收到错误.怎么用Bash和getopt

bash shell getopts

310
推荐指数
7
解决办法
44万
查看次数

命令以毫秒为单位获取时间

Linux中是否有shell命令以毫秒为单位获取时间?

linux bash shell time

257
推荐指数
9
解决办法
32万
查看次数

如何杀死僵尸进程

我在前台启动了我的程序(一个守护程序),然后我用它杀了它kill -9,但我得到了一个僵尸,我无法杀死它kill -9.如何杀死僵尸进程?

如果僵尸是一个死的进程(已经被杀死),我如何从输出中删除它ps aux

root@OpenWrt:~# anyprogramd &
root@OpenWrt:~# ps aux | grep anyprogram
 1163 root      2552 S    anyprogramd
 1167 root      2552 S    anyprogramd
 1169 root      2552 S    anyprogramd
 1170 root      2552 S    anyprogramd
10101 root       944 S    grep anyprogram
root@OpenWrt:~# pidof anyprogramd
1170 1169 1167 1163
root@OpenWrt:~# kill -9 1170 1169 1167 1163
root@OpenWrt:~# ps aux |grep anyprogram
 1163 root         0 Z    [cwmpd]
root@OpenWrt:~# kill -9 1163
root@OpenWrt:~# ps aux |grep anyprogram
 1163 …
Run Code Online (Sandbox Code Playgroud)

linux shell ubuntu debian zombie-process

169
推荐指数
5
解决办法
41万
查看次数

如何在进程fork()之间共享内存?

在fork子中,如果我们修改一个全局变量,它将不会在主程序中被更改.

有没有办法在子fork中更改全局变量?

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int glob_var;

main (int ac, char **av)
{
  int pid;

  glob_var = 1;

  if ((pid = fork()) == 0) {
    /* child */
    glob_var = 5;
  }
  else {
    /* Error */
    perror ("fork");
    exit (1);
  }

  int status;
  while (wait(&status) != pid) {
  }
   printf("%d\n",glob_var); // this will display 1 and not 5.
}
Run Code Online (Sandbox Code Playgroud)

c fork

56
推荐指数
2
解决办法
7万
查看次数

git:'send-email'不是git命令.看'git --help'

我正在尝试发送补丁git send-email但是我得到以下错误:

git: 'send-email' is not a git command. See 'git --help'.
Run Code Online (Sandbox Code Playgroud)

怎么做git send-email作品?

相关链接:

http://www.kernel.org/pub/software/scm/git/docs/git-send-email.html

git

55
推荐指数
1
解决办法
2万
查看次数

如何使用sed提取子字符串

我有一个包含以下行的文件:

  <parameter name="PortMappingEnabled" access="readWrite" type="xsd:boolean"></parameter>
  <parameter name="PortMappingLeaseDuration" access="readWrite" activeNotify="canDeny" type="xsd:unsignedInt"></parameter>
  <parameter name="RemoteHost" access="readWrite"></parameter>
  <parameter name="ExternalPort" access="readWrite" type="xsd:unsignedInt"></parameter>
  <parameter name="ExternalPortEndRange" access="readWrite" type="xsd:unsignedInt"></parameter>
  <parameter name="InternalPort" access="readWrite" type="xsd:unsignedInt"></parameter>
  <parameter name="PortMappingProtocol" access="readWrite"></parameter>
  <parameter name="InternalClient" access="readWrite"></parameter>
  <parameter name="PortMappingDescription" access="readWrite"></parameter>
Run Code Online (Sandbox Code Playgroud)

我想在此文件上执行命令以仅提取以下输出中显示的参数名称:

$sedcommand file.txt
PortMappingEnabled
PortMappingLeaseDuration
RemoteHost
ExternalPort
ExternalPortEndRange
InternalPort
PortMappingProtocol
InternalClient
PortMappingDescription
Run Code Online (Sandbox Code Playgroud)

这个命令可能是什么?

linux shell ubuntu sed xml-parsing

48
推荐指数
5
解决办法
13万
查看次数

pthread_cond_wait(&cond_t,&mutex); 解锁然后锁定互斥锁?

pthread_cond_wait(&cond_t, &mutex);在我的程序中使用,我想知道为什么这个函数需要作为第二个参数mutex变量.

是否pthread_cond_wait()在开始时解锁互斥锁(执行开始pthread_cond_wait())然后在完成时(就在离开之前pthread_cond_wait())锁定?

c mutex pthreads

38
推荐指数
3
解决办法
3万
查看次数

如何在git补丁中添加Signed-off-by字段

我正在生成一个git补丁.然后我想将它发送给项目维护者.

我想signed-off-by在git补丁中标记我的姓名和电子邮件地址

怎么做 ?为了让项目维护者获得signed-off-by带有我的姓名和电子邮件地址的字段.

git

31
推荐指数
1
解决办法
2万
查看次数

如何使用linux命令显示我文件中每行的第一个单词?

我有一个包含许多行的文件,我想用Linux命令只显示每行的第一个单词.

我怎样才能做到这一点?

linux shell grep

24
推荐指数
3
解决办法
6万
查看次数

是否可以在同一行中打印awk输出

awk输出看起来像:

awk '{print $2}'
toto
titi
tata
Run Code Online (Sandbox Code Playgroud)

我想在同一行中以空格作为分隔符而不是新行来显示awk的输出

awk [option] '{print $2}'
toto titi tata
Run Code Online (Sandbox Code Playgroud)

有可能吗?

linux bash shell awk

19
推荐指数
3
解决办法
3万
查看次数

标签 统计

shell ×6

linux ×5

bash ×3

c ×2

git ×2

ubuntu ×2

awk ×1

debian ×1

fork ×1

getopts ×1

grep ×1

mutex ×1

pthreads ×1

sed ×1

time ×1

xml-parsing ×1

zombie-process ×1