VRK
VRK

Reputation: 1366

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.

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.

Upvotes: 1

Views: 178

Answers (1)

anubhava
anubhava

Reputation: 786081

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.

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.

Upvotes: 1

Related Questions