-1

Script:

 #!/bin/bash

 mydir="/bamboo/artifacts"
 cd "$mydir"
 job="-JOB1"

 for dir in */
    do
       bambooplan=`echo $dir | sed 's/\/$//g'`$job
       echo $bambooplan
    done

Output:

   LERST-TSTREDAPIDB2WIN4-JOB1
   LERST-TSTREDAPIDB2WIN5-JOB1
   CA-TSTALLSQLWIN3-JOB1
   CE-CSW-JOB1
   CE-SNAP-JOB1

I want to pass this to a file. I tried the commands below and it gives permission denied error. Could someone help me figure what I miss here?

echo $bambooplan > result.txt
$bambooplan > result.txt
VRK
  • 1,206
  • 3
  • 13
  • 27
  • Not an exact duplicate, but look at this: http://stackoverflow.com/questions/18612603/redirecting-output-of-bash-for-loop. And http://mywiki.wooledge.org/BashFAQ/014 – Benjamin W. Feb 05 '16 at 22:30
  • Your syntax is correct. Permission denied means you don't have write permission to `result.txt`, or the file doesn't exist and you don't have write permission to the directory. – Barmar Feb 05 '16 at 23:22

2 Answers2

1

I tested your code with the first 'writing line' (echo $bambooplan > result.txt) and worked perfect for me, only i would change echo $bambooplan > result.txt to echo $bambooplan >> result.txt, otherwise, you will overwrite the entire file in each iteration, keeping only the last one.

Be aware that your code writes INSIDE /bamboo/artifacts and you wrote the file path as /bamboo/artifacts, an absolute path, not a relative one -> bamboo/artifacts, so, maybe, you're trying to write in a folder where you don't have writing permissions and need to chmod them.

Ghost
  • 1,426
  • 5
  • 19
  • 38
0

Redirect just to your defined file by using > as you specified or >> if file already already exists and contains data. Regarding to the permission, just exchange script's rights thru chmod command.

for instance :

chmod 755 scriptname

Same process to change output file's permission.

ogs
  • 1,139
  • 8
  • 19
  • 42