标签: shell

Find common lines to multiple files

I have nearly 200 files and I want to find lines that are common to all 200 files,the lines are like this:

HISEQ1:105:C0A57ACXX:2:1101:10000:105587/1
HISEQ1:105:C0A57ACXX:2:1101:10000:105587/2
HISEQ1:105:C0A57ACXX:2:1101:10000:121322/1
HISEQ1:105:C0A57ACXX:2:1101:10000:121322/2
HISEQ1:105:C0A57ACXX:2:1101:10000:12798/1
HISEQ1:105:C0A57ACXX:2:1101:10000:12798/2
Run Code Online (Sandbox Code Playgroud)

is there a way to do it in a batch way?

bash shell awk sed

3
推荐指数
1
解决办法
610
查看次数

Python:环境变量未更新

我正在尝试覆盖 Python 中的环境变量。我可以读取该值,然后写入该值并打印更新后的值。但是,如果我检查命令行中的值,它仍然是原始值。这是为什么?

首先,我创建变量

export MYVAR=old_val
Run Code Online (Sandbox Code Playgroud)

我的测试脚本 myvar.py

#!/usr/bin/env python3
import os
print (os.environ['MYVAR'])
os.environ['MYVAR'] = "new_val"
print (os.environ['MYVAR'])
Run Code Online (Sandbox Code Playgroud)

输出

$ ./myvar.py 
old_val
new_val
$ echo $MYVAR
old_val
Run Code Online (Sandbox Code Playgroud)

如您所见,输出的最后一行仍然显示 old_val

python shell environment-variables

3
推荐指数
1
解决办法
1793
查看次数

如何将 docker ENTRYPOINT 与 shell 脚本文件组合参数一起使用

我编写了 shell 脚本文件并将其与 docker ENTRYPOINT 一起使用,但是当我运行 docker image 时,它​​会因为入口点代码行而停止而没有任何错误日志

我的 Dockerfile

FROM ubuntu:16.04
MAINTAINER limtaegeun <imori333@gmail.com>

RUN apt-get update
RUN apt-get install -y nginx
RUN echo "\ndaemon off;" >> /etc/nginx/nginx.conf

# Define mountable directories.
VOLUME ["/etc/nginx/sites-enabled", "/etc/nginx/certs", "/etc/nginx/conf.d", "/var/log/nginx", "/var/www/html"]

ENV CONTAINER_NAME nodejs
ENV SERVER_NAME myserver.com
ENV PEM_PATH /etc/nginx/certs/cert.pem
ENV KEY_PATH /etc/nginx/certs/cert.key

WORKDIR /etc/nginx

ADD ./sites-available/ssl /etc/nginx/sites-available/ssl
ADD ./docker-entrypoint.sh /etc/nginx/docker-entrypoint.sh
RUN chmod 777 /etc/nginx/docker-entrypoint.sh
EXPOSE 80 443
ENTRYPOINT /etc/nginx/docker-entrypoint.sh ${CONTAINER_NAME} ${SERVER_NAME} ${PEM_PATH} ${KEY_PATH}
CMD ["nginx"]
Run Code Online (Sandbox Code Playgroud)

docker-entrypoint.sh

#!/bin/sh

CONTAINER_NAME=$1
SERVER_NAME=$2
PEM_PATH=$3 …
Run Code Online (Sandbox Code Playgroud)

bash shell docker

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

exit(1) 没有给我 1 作为退出值?

为什么当我在 c 中的函数中执行此代码时

int Pandoc(char *file)
{

    //printf("Pandoc is trying to convert the file...\n");

    // Forking
    pid_t pid;
    pid = fork();

    if (pid == -1)
    {
        perror("Fork Error");
    }
    // child process because return value zero
    else if (pid == 0)
    {

        //printf("Hello from Child!\n");
        // Pandoc will run here.

        //calling pandoc
        // argv array for: ls -l
        // Just like in main, the argv array must be NULL terminated.
        // try to run ./a.out -x -y, it will work …
Run Code Online (Sandbox Code Playgroud)

c shell exit-code exit

3
推荐指数
1
解决办法
81
查看次数

Shell 脚本:使用自己的 IP 地址作为变量

我必须编写一个 shell 脚本来初始化 docker swarm。

我需要将自己的 IP 地址作为参数传递:

 sudo docker swarm init --advertise-addr 167.172.176.134
Run Code Online (Sandbox Code Playgroud)

我怎样才能动态地做到这一点?我知道该ifconfig命令,它允许我查看 IP,但我无法将整个内容作为变量传递。它应该是这样的

ip = ifconfig["IP"]
sudo docker swarm init --advertise-addr $ip
Run Code Online (Sandbox Code Playgroud)

linux bash shell

3
推荐指数
1
解决办法
70
查看次数

如何在 if 语句中使用 REGEX

我需要创建一个代码,其中我获取当前目录中以 *.txt、*.jpg、*.conf 结尾的所有文件。在我收集这些之后,我需要检查它们是否包含某一行,即:
<?xml version="1.0" encoding="UTF-8"?>
如果它们确实包含以下行,那么我需要将它们重命名为 *.xml。


我的问题是:如果文件包含给定的正则表达式代码,我如何在 if 语句中检查正则表达式?

我目前的代码:

ls *.txt *.data *.conf
for f in *.*
case $f in
*.txt)
if [[ "$(grep -q '<\?xml version="[0-9]*.[0-9]*" encoding=".*"\?>' *.txt) ]]; then
mv -- "$f" "${%f.txt].xml"
fi
;;

esac
done
ls
Run Code Online (Sandbox Code Playgroud)

*.jpg) 和 *.conf) 的代码是相同的。任何帮助表示赞赏。

bash shell sh

3
推荐指数
1
解决办法
44
查看次数

UNIX:从带分隔符的文本文件创建表格格式的输出

我需要从文本文件中获取表格格式输出,我正在通过以下 awk 命令实现它。

分隔文件

ACTIVE#1238917238971238#USA#The U.S. is a country of 50 states covering a vast swath of North America.
ACTIVE#21389721839781237812#INDIA#India, officially the Republic of India, is a country in South Asia.
ACTIVE#3121278372183782137812#AUSTRALIA#Australia, officially the Commonwealth of Australia, is a sovereign country comprising the mainland of the Australian continent
Run Code Online (Sandbox Code Playgroud)

AWK 命令

awk -F"#" 'BEGIN {{printf "%-80s\n","--------------------------------------------------------------------------------------------------------------------------------------"} {printf "|%-12s|%-30s|%-38s|%-50s|\n","STATUS","ID", "Country", "Description"} {printf "%-80s\n","--------------------------------------------------------------------------------------------------------------------------------------"}} {printf "|%-12s|%-30s|%-38s|%-50s|\n",$1,$2,$3,$4} END{printf "%-80s\n","--------------------------------------------------------------------------------------------------------------------------------------"}' /tmp/test.txt
Run Code Online (Sandbox Code Playgroud)

输出: 在此处输入图片说明

如果您可以看到 Description 列的输出,则它不会在其自己的列中格式化输出,而是由于字符串长度而弄乱了整个表。

有人可以查看并建议我如何更好地显示说明列的输出吗?

unix shell awk

3
推荐指数
1
解决办法
93
查看次数

如何:# use perl eval 'exec perl -S $0 ${1+"$@"}' 设置 perl 版本?

我必须调试一个旧的 perl 脚本,它显然是以一种我不理解的方式设置 perl 的版本......

: # use perl
    eval 'exec perl -S $0 ${1+"$@"}'
    if 0;
Run Code Online (Sandbox Code Playgroud)

perl -h说这-S意味着“使用 PATH 环境变量查找程序文件”。 $0是当前程序。我读过这$@意味着“来自最后一个eval命令的 Perl 语法错误消息”。但是他们为什么要加 1 呢?这一切是如何结合在一起的?它在做什么?

我必须调试的部分内容与它选择了我不想要的旧版本 perl 的事实有关。对于其他一切,我们使用#!/usr/bin/env perl它,我怀疑,可能会做同样的事情。我还怀疑我的解决方案可能在于修复$PATH(或防止弄乱它的代码弄乱它)。但我想通过更好地了解它现在如何选择版本来解决这个问题。

谢谢你的帮助 !

shell perl

3
推荐指数
1
解决办法
221
查看次数

如何切割由变量定义的一系列线

我有这个python爬虫输出

[+] Site to crawl: http://www.example.com
[+] Start time: 2020-05-24 07:21:27.169033
[+] Output file: www.example.com.crawler

[+] Crawling
   [-] http://www.example.com
   [-] http://www.example.com/
   [-] http://www.example.com/icons/ubuntu-logo.png
   [-] http://www.example.com/manual
    [i] 404 Not Found
[+] Total urls crawled: 4

[+] Directories found:
   [-] http://www.example.com/icons/
[+] Total directories: 1

[+] Directory with indexing
Run Code Online (Sandbox Code Playgroud)

我想使用 awk 或任何其他工具在“爬行”和“爬行的总网址”之间划清界限,所以基本上我想使用变量将 NR 分配给第一个关键字“爬行”,并将第二个变量分配给它 NR第二个限制器“爬行的总网址”的值,然后削减两者之间的范围,我尝试了这样的事情:

awk 'NR>$(Crawling) && NR<$(urls)' file.txt
Run Code Online (Sandbox Code Playgroud)

但没有什么真正奏效,我得到的最好的是从 Crawling+1 行到文件末尾的剪切,这实际上没有帮助,所以如何做以及如何使用带有变量的 awk 剪切一系列行!

awk

shell awk cut range

3
推荐指数
1
解决办法
74
查看次数

如何从鱼壳的命令历史记录中删除条目?

在尝试解锁屏幕时,我不小心将密码输入到运行 fish shell 的终端中。

如何再次从鱼的命令历史记录中删除它?

该解决方案不需要在取证上是安全的。备份文件或驱动器上留下的痕迹对我来说很好。我只是想避免例如当我在共享时使用鱼的历史记录时我的密码在屏幕共享会话中意外弹出。而且我想在历史中保留所有其他命令,因为我经常从历史中回忆复杂的命令,有时在重用之前对其进行编辑。

shell input-history fish

3
推荐指数
1
解决办法
400
查看次数

标签 统计

shell ×10

bash ×4

awk ×3

c ×1

cut ×1

docker ×1

environment-variables ×1

exit ×1

exit-code ×1

fish ×1

input-history ×1

linux ×1

perl ×1

python ×1

range ×1

sed ×1

sh ×1

unix ×1