-3

Hi I've been having trouble with this piece of code for about a day now and I still keep getting the same error which is this:

enter image description here

Here's the code behind it: https://pastebin.com/GpAwrcbj

for f in $(ls $repo); do for f_ghost in $(ls $repo".repo_files/ghost_repo/"); do
    echo $f" "$f_ghost
    if ("$($f)" == "$($f_ghost)")
    then

I've been trying to figure out whether it's something to be with the way I'm comparing the two file names but even when trying to use the bash guide for operators it's gotten me nowhere. If anyone has any suggestions behind what could be causing this. (I've tried chmod to make sure everything has rw access for all users btw so I honestly have no idea why this is giving a perms error)

Alfabravo
  • 7,493
  • 6
  • 46
  • 82
Xerion
  • 3
  • 2
  • 3
    Run your code through shellcheck.net. – chepner Oct 11 '17 at 21:26
  • 2
    If it tells you that you don't have permissions and/or the files were not found, go a step backwards and check the result of `ls $repo` – Alfabravo Oct 11 '17 at 21:27
  • 2
    Have you ever tried to run your script in debug mode (`-x`)? – Jdamian Oct 11 '17 at 21:30
  • 1
    You're not comparing strings, you're running a command in a subshell. `$($f)` means "run the command named `$f` in a subshell". Comparing strings is done by `[ "$f" = "$f_ghost" ]`. See https://stackoverflow.com/questions/2237080/how-to-compare-strings-in-bash. This also explains the permissions error: It's trying to *execute* the filename, so setting RW permissions won't help you there. – bnaecker Oct 11 '17 at 21:36
  • 1
    BTW, see [why you shouldn't parse the output of `ls`](http://mywiki.wooledge.org/ParsingLs) – Charles Duffy Oct 11 '17 at 21:37
  • "It doesn't work? Let's throw more syntax at it!" -- at some point, it's worth writing something direct from docs rather than guessing at syntax. (To be clear, @bnaecker hit the nail on the head in terms of where the most immediate problem is). – Charles Duffy Oct 11 '17 at 21:39

1 Answers1

0

I don't think you are doing what you think you are doing in the if. Parentheses in bash just runs the command inside in a subshell. So you are trying to run the two arguments as commands. (The $($f) bit will expand $f, try to run it, and then substitute the result, expecting that to be a command.)

To compare strings, you probably want to do something like

if [ "$f" = "$f_ghost" ]

The rest of the code looks a bit strange too, but I suggest you start there.

chepner
  • 497,756
  • 71
  • 530
  • 681
Göran Uddeborg
  • 351
  • 3
  • 10
  • Thank you so much that error was driving me insane!!! It's my first time coding in bash and a lot of the code I find online doesn't really tell why it's differentiating between square brackets and parenthesis. – Xerion Oct 12 '17 at 14:53
  • Also yeah the rest of the code was a mess but this was the main error that I needed help with. I kinda went down a hole trying to make the if statement work that I made tons of other syntax errors in the code for joining variables and strings. – Xerion Oct 12 '17 at 15:34