我要编写一个bash脚本,它返回标题和文档类型以及给定的PDF文件列表中的一些其他内容.所以我试图编写一个函数来获取文档类型.当文档类型为ACM或MIT时,它可以正常工作,但是当输出处于elif块时,它会显示"未找到命令".我的代码在这里 -
#!/bin/bash
function get_type(){
if less "$1" | grep -q -i "ACM TRANSACTIONS"
then
type="ACM Transactions"
elif less "$1" | grep -q -i "journal homepage: www.elsevier.com"
then
type= "ELSEVIER"
elif less "$1" | grep -q -i "IEEE TRANSACTIONS"
then
type= "IEEE Transactions"
else
type="MIT Press"
fi
echo $type
}
for file in ~/Desktop/1105063/papers/*;
do
get_type "$file"
done
Run Code Online (Sandbox Code Playgroud)
这是输出 -
shawon@Shawon-Linux:~/Desktop/1105063$ ./test.sh
./test.sh: line 12: IEEE Transactions: command not found
[...]
Run Code Online (Sandbox Code Playgroud) 我试图pid在运行fork()命令后打印进程.这是我的代码 -
#include <stdio.h>
#include <unistd.h>
int main(void)
{
int pid;
pid=fork();
if(pid==0)
printf("I am the child.My pid is %d .My parents pid is %d \n",getpid(),getppid());
else
printf("I am the parent.My pid is %d. My childs pid is %d \n",getpid(),pid);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这是我得到的答案 -
I am the parent.My pid is 2420. My childs pid is 3601
I am the child.My pid is 3601 .My parents pid is 1910
Run Code Online (Sandbox Code Playgroud)
为什么父母不在第二行.2420为什么我得到我1910怎么能得到这个价值?
我有以下代码:
#!/bin/sh
LOG="myfile.log"
ls -l "${LOG}_*"
find . -type f -name "${LOG}_*" -exec ls -l {} \;
Run Code Online (Sandbox Code Playgroud)
产生以下输出:
$ ./test.sh
ls: cannot access myfile.log_*: No such file or directory
-rw-r--r--. 1 user user 0 Sep 26 23:03 ./myfile.log_20150926_1928
-rw-r--r--. 1 user user 0 Sep 26 23:03 ./myfile.log_20150926_1926
-rw-r--r--. 1 user user 0 Sep 26 23:03 ./myfile.log_20150926_1930
-rw-r--r--. 1 user user 0 Sep 26 23:03 ./myfile.log_20150926_1927
-rw-r--r--. 1 user user 0 Sep 26 23:03 ./myfile.log_20150926_1929
Run Code Online (Sandbox Code Playgroud)
我不明白为什么"find"命令工作而不是"ls"
干杯
我的bash脚本一直失败,因为它在连字符处混淆:
if [ ! -d "$openssl-1.0.1i"]; then ...
Run Code Online (Sandbox Code Playgroud)
我该如何正确逃脱它?
我正在考虑编写一个脚本,将所有有用的文件复制到临时文件夹和zip临时文件夹中.有更简单的方法吗?
我经常想要获取一个文件的前1000行,比如说,另一个文件的前200行,并将连接管道传递给文件.
要做到这一点我能做到
head -1000 fileA > fileA-1000
head -200 fileB > fileB-200
cat fileB-200 fileA-1000 > file C
Run Code Online (Sandbox Code Playgroud)
有可能作为一个命令完成所有这些吗?
我在一些脚本中看到有一个连接到shell变量的连字符.例如:
if [ -z ${X-} ]
Run Code Online (Sandbox Code Playgroud)
变量旁边的这个连字符符号在这里做什么.我在网上找不到任何相关文档.
我需要一个Shell脚本来打开给定目录中的最新文本文件。然后将其复制到另一个目录。我该如何实现?
我需要一种逻辑,该逻辑将从目录中搜索并提供最新文件(文本文件的名称可以是任何数字(不固定),因此我需要找出最新的文本文件)
我有一个简单的bash脚本,可以监视mysql是up还是down.如果它关闭,我想也阻止HAProxy运行.下面是我简单的bash脚本:
#!/bin/bash
nc -z 127.0.0.1 3306
rt_val=$?
msg=$(sudo service haproxy stop);
if [ $rt_val != 0 ]; then
eval $msg
exit 3
else
exit 0
fi
Run Code Online (Sandbox Code Playgroud)
MySQL运行或不运行的部分工作得很好.停止HAProxy部分似乎有问题.发生的事情是当mysql停止时HAProxy停止.但是当我启动MySQL和HAProxy时,即使MySQL启动并运行,脚本似乎也会继续停止HAProxy.
//Executing shell command ls -l | sort
int *pipeIN, *pipeOUT;
int runLS(){
char* parmListLS[] = { "ls", "-l", NULL };
int pid = fork();
if(pid==0){
close(*pipeIN);
dup2(*pipeOUT, STDOUT_FILENO);
execvp(parmListLS[0], parmListLS);
}else return pid;
}
int runSORT(){
char* parmListSORT[] = { "sort", NULL };
int pid = fork();
if(pid==0){
close(*pipeOUT);
dup2(*pipeIN, STDIN_FILENO);
execvp(parmListSORT[0], parmListSORT);
}else return pid;
}
int main(void){
int pidLS, pidSort, pipeId[2];
pipeIN = &pipeId[0], pipeOUT = &pipeId[1];
pipe(pipeId); //open pipes
pidLS = runLS();
pidSort = runSORT();
printf("PIDS: LS …Run Code Online (Sandbox Code Playgroud)