0

I was trying to assign a variables for text files in shell script but was not able to to it.

If i have three text files one.txt, two.txt,three.txt

I wrote these variables in my source.sh file assigning for these three txt files

#!/bin/bash
abc="path/one.txt";
pqr="path/two.txt";
xyz="path/three.txt";

Then I used the following command to run shell script xyz.sh with above variables

bash xyz.sh user/bin/path abc pqr

wherein xyz.sh is as follows

#!/bin/bash

SAVEPATH=$1;
TEXTFILE1=$2;
TEXTFILE2=$3;


MY_ROOT=$(cat $SAVEPATH/.txt file containing PATH TO MY ROOT);
source $MY_ROOT/source.sh  $MY_ROOT;

calculator coverage -count -a $TEXTFILE1 -b $TEXTFILE2 > output.txt;

However I am not able to get the desired output. This is just simplified example. How to solve this issue?

RonicK
  • 229
  • 2
  • 3
  • 10
  • What behavior *do* you get? If you run `bash -x yourscript`, at which point does the log of commands it's running differ from what you expect/intend? – Charles Duffy May 07 '18 at 15:46
  • BTW, consider running your code through http://shellcheck.net/ and fixing what it finds (at minimum there are some quoting bugs; whether they'll actually break anything depends on the details of your filenames/contents). – Charles Duffy May 07 '18 at 15:47
  • It fails to detect TEXTFILE1 and TEXTFILE2 – RonicK May 07 '18 at 15:48
  • "Fails to detect" means what, specifically? Does the output logged by `bash -x xyz.sh user/bin/path abc pqr` not show your arguments being passed through? What *does* it show in place thereof? Moreover, what does it log for the lines assigning `TEXTFILE1` and `TEXTFILE2`? – Charles Duffy May 07 '18 at 15:48
  • BTW, as an aside, all-caps variables are used for names meaningful to the shell itself and to POSIX-defined utilities, whereas variables with lowercase names are reserved for application use and guaranteed not to conflict with the behavior of shell & utilities (for POSIX-compliant shells; zsh isn't compliant, and doesn't follow that rule). See http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap08.html, fourth paragraph. – Charles Duffy May 07 '18 at 15:51
  • output.txt file generated is blank, however if I put directly textfiles one.txt and two.txt instead of $TEXTFILE1 and $TEXTFILE2 it generates desired output. – RonicK May 07 '18 at 15:52
  • Please answer the question about logs generated by `bash -x yourscript`, **or** provide a tested [mcve] someone who doesn't have your `calculator` command can use to see the problem themselves. – Charles Duffy May 07 '18 at 15:52
  • Oh. **Oh**. You want indirect expansion. Arright, that's something we have Q&A entries already in the knowledgebase for. – Charles Duffy May 07 '18 at 15:54
  • "behavior" and "log" aren't technical terms -- I'm using them with their standard English-language meanings (in the same sense that someone writes a record of what they've done in a "log book", running `bash -x yourscript` will have the interpreter write a record of everything it does to stderr; [edit]ing that record into the question would make the issue at hand considerably clearer). That said, to spoon-feed an answer: `calculator coverage -count -a "${!TEXTFILE1}" -b "${!TEXTFILE2}"` – Charles Duffy May 07 '18 at 15:59
  • Thanks Professor. :) – RonicK May 07 '18 at 16:15

0 Answers0