简体   繁体   English

在shell脚本中将文本文件的值传递给数组

[英]passing values of text file to array in shell scripting

My script fetches the names of directories in a path and stores in a text file. 我的脚本获取路径中的目录名称,并将其存储在文本文件中。

#!/bin/bash
MYDIR="/bamboo/artifacts"
DIRS=`ls -d /bamboo/artifacts/* | cut -d'/' -f4 > plan_list.txt`

plan_list.txt:
**************

PLAN1
PLAN2
PLAN3

Now I am trying to pass each of these directory names to a URL to get output like this. 现在,我尝试将这些目录名称中的每一个传递给URL,以获取这样的输出。

http://bamboo1.test.com:8080/browse/PLAN1
http://bamboo1.test.com:8080/browse/PLAN2
http://bamboo1.test.com:8080/browse/PLAN3    

The script to do that doesn't seem to work 这样做的脚本似乎不起作用

bambooServer="http://bamboo1.test.com:8080/browse/"
for DIR in $DIRS
do
  echo `$bambooServer+$DIR`
done

Could someone please tell me what I am missing here? 有人可以告诉我我在这里想念的吗? Instead of storing the ls command output to a plan_list.txt file i tried passing to array but that didn't work well too. 我没有将ls命令的输出存储到plan_list.txt文件中,而是尝试传递给array,但是效果也不好。

DIRS=`ls -d /bamboo/artifacts/* | cut -d'/' -f4 > plan_list.txt`

DIRS is just an empty variable since your command is not producing any output and just redirecting output to plan_list.txt . DIRS只是一个空变量,因为您的命令不会产生任何输出,只会将输出重定向到plan_list.txt

You can rewrite your script like this: 您可以这样重写脚本:

#!/bin/bash

mydir="/bamboo/artifacts"
cd "$mydir"

bambooServer="http://bamboo1.test.com:8080/browse/"
for dir in */
do
  echo "$bambooServer$dir"
done

*/ is the glob pattern to get all the directories in your current path. */是全局模式,用于获取当前路径中的所有目录。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM