相关疑难解决方法(0)

SHELL 在特定变量的开头和结尾添加双引号

我在 shell 脚本中有一个变量,它在变量 $var1 中取一个值。如果我这样做,echo $var1我将获得例如价值波士顿。我想要的值是"Boston"

我尝试了几种情况:

var1=""$var1""
var1="""$var1"""
Run Code Online (Sandbox Code Playgroud)

但在这两种情况下,我都得到了结果,'$var1'但我想要"Boston" 解决方案请不要提及其中包含波士顿的解决方案。如果您只能var1在整个脚本中使用,我将不胜感激,这样可以更清楚!“Boston”只需要是脚本执行的输出和变量 var1 的初始化。

谢谢

variables bash shell double-quotes

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

使用循环变量名重命名文件

我正在尝试下载一堆文件,并在我去的时候重命名它们.下载部分工作正常,但我不能让它重命名文件.这是我正在使用的脚本:

COUNTER=0
for (( i = 696; i <= 773; i++ ))
  do
  fastq-dump --split-3 --accession SRR546$i
mv SRR546"$i"_1 ./"$COUNTER"mVD_SRR546$i_1
mv SRR546"$i"_2 ./"$COUNTER"mVD_SRR546$i_2
gzip *.fastq

COUNTER=$[COUNTER + 1]
done
Run Code Online (Sandbox Code Playgroud)

这将正确下载文件SRR546696,SRR546697等,但不会重命名它们.

我也试过用:

rename 's/SRR/"$COUNTER"mVD_SRR/' *.fastq
Run Code Online (Sandbox Code Playgroud)

但这也没有做任何事.

bash loops rename

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

为什么 shell 脚本中的“读取”命令缺少初始字符?

我有以下 shell 脚本,除了第一行之外,它在每一行中都缺少一些初始字符(到目前为止,在我的观察中它缺少最初的几个字符)。

只有当我使用ffmpeg命令时才会发生这种情况。否则,它很好。但是此命令执行此脚本中的实际任务。

为什么会这样,解决方法是什么?

#!/bin/bash

while read line; do
    printf "%s\n" "$line" 
    ifile=$line
    printf "%s\n" "$ifile" 
    ofile=abc_$line
    printf "%s\n" "$ofile" 

    ############### Problem is the following command: ##########
    ffmpeg -y -i $ifile -c:v libx264rgb -b:v 512k -bf 0 -pix_fmt rgb24  -r 25 -strict -2 $ofile
    ##########rest is fine##########

    echo $ifile
done < file_list
Run Code Online (Sandbox Code Playgroud)

linux bash shell ubuntu ffmpeg

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

在grep之后的每行开头添加文本

我正在尝试使用以下bash脚本提取github中所有存储库的名称并构建一个脚本文件来克隆所有存储库:

for i in {1..10}
do
curl -u USERNAME:PASS -s https://api.github.com/user/repos?page=$i | grep -oP '"clone_url": "\K(.*)"' > output$i.txt
done
Run Code Online (Sandbox Code Playgroud)

这是我的脚本在单行中输出每个存储库名称,但是我需要在每行的开头插入git clone,所以我写了这个(添加了| xargs -L1 git clone),该命令不起作用:

for i in {1..10}
do
curl -u USERNAME:PASS -s https://api.github.com/user/repos?page=$i | grep -oP '"clone_url": "\K(.*)"' | xargs -L1 git clone > output$i.txt
done
Run Code Online (Sandbox Code Playgroud)

bash shell grep github-api

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

在子目录中查找和gzip文件

我在位置/ home/Docs/Calc中有*.xls文件.该文件夹中有多个子目录.例如

/home/Docs/Calc/2011
/home/Docs/Calc/2012
/home/Docs/Calc/2013
Run Code Online (Sandbox Code Playgroud)

我可以使用find命令在子目录下gzip每个文件,

find /home/Docs/Calc -iname "*.xls" -exec gzip {} \;
Run Code Online (Sandbox Code Playgroud)

但是我如何gzip每个子目录中的所有文件?例如.

/home/Docs/Calc/2011/2011.tar.gz
/home/Docs/Calc/2012/2012.tar.gz
/home/Docs/Calc/2013/2013.tar.gz
Run Code Online (Sandbox Code Playgroud)

我必须补充一点/ home/Docs/Calc是众多文件夹中的一个,例如Calc-work,calc-tax,calc-bills.所有这些都包含年份子文件夹

bash

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

curl:JSON 中的意外令牌

当我尝试提交此内容时,服务器将消息发回给我

"Unexpected token t in JSON at position 1".

无论第一个非 { 非 " 字符是什么,它都会这样做。如果我输入 {"": ""} 它会说

"Unexpected token : in JSON at position 1".
Run Code Online (Sandbox Code Playgroud)

这是我的代码。

  curl -H "Content-Type: application/json" -d {"test": "test"} url
Run Code Online (Sandbox Code Playgroud)

我已经尝试了在它周围加上引号并转义引号的其他变体,但无济于事,但如果您认为这是问题,请随时提出一种格式。

bash json curl

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

bash脚本中$1和“$1”有什么区别?

例子

while [ -n "$1" ]
do
  something
done
Run Code Online (Sandbox Code Playgroud)

我可以写$1代替吗"$1"user=alexander和和有什么区别 user="alexander"?谢谢

bash

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

查找内容相同的文件

使用 Kubator 命令行回答我的问题:

 #Function that shows the files having the same content in the current directory
showDuplicates (){
  last_file=''
  while read -r f1_hash f1_name; do
    if [ "$last_file" != "$f1_hash" ]; then
      echo "The following files have the exact same content :"
      echo "$f1_name"
      while read -r f2_hash f2_name; do
        if [ "$f1_hash" == "$f2_hash" ] && [ "$f1_name" != "$f2_name" ]; then
          echo "$f2_name"
        fi
      done < <(find ./ -maxdepth 1 -type f -print0 | xargs -0 md5sum | …
Run Code Online (Sandbox Code Playgroud)

bash

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

如何在bash中使用多线程附加多个文件

我们如何使用多线程将多个文件追加到单个文件中,我的每个文件都有10M的行。所以我想同时处理所有文件?

 #!/bin/bash
appendFiles  A.TXT &
appendFiles  B.TXT &
appendFiles  C.TXT &
wait

function appendFiles 
 {
while  read -r line; do
echo $line >>final.txt
done < $1
} 
Run Code Online (Sandbox Code Playgroud)

unix bash shell sh

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

如何在bash脚本中传递字符串引号?

在命令行上,如果我键入

adb -s "" shell
Run Code Online (Sandbox Code Playgroud)

我有一个外壳,但是如果我尝试在bash中这样做:

#!/bin/bash
ADB_ID=""
adb -s ${ADB_ID} shell
Run Code Online (Sandbox Code Playgroud)

我得到一个错误。我了解它没有传递$ {ADB_ID}的任何内容。我尝试将引号转义,这导致它查找名为“”的设备,这是不正确的。我也尝试过使用单引号和单转义的引号,两者都是错误的。如何在bash脚本中传递等效的命令行?

bash

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

标签 统计

bash ×10

shell ×4

curl ×1

double-quotes ×1

ffmpeg ×1

github-api ×1

grep ×1

json ×1

linux ×1

loops ×1

rename ×1

sh ×1

ubuntu ×1

unix ×1

variables ×1