0

I want to run this command on all of the files that I have in a directory

twarc hydrate "FileInputName" > "FileOutput_Num.json"

I wrote this bash script, but I get a lot of errors.

#!/bin/bash

num=0

for FILE in  /Users/Chase/Desktop/twitter_hydration/Beyond the Hashtags/*.txt ; do
    twarc hydrate $file tweets$(num).jsonl
    $num = $num + $num
done

I'm new to this so any help is appreciated.

edit.

#!/bin/bash

num=0
for file in *.txt ; do
    twarc hydrate "$(file)" > tweets"$(num)".jsonl
    num=$(( num + num ))
done
Chase
  • 33
  • 2
  • 11
  • `$(num)` is a very different thing from `${num}`, and `$num = $num + $num` is very different from `num=$(( num + num ))`. And you need to quote the spaces in your directory name. – Charles Duffy Aug 02 '18 at 22:09
  • And you also need to quote your expansions. Run this through http://shellcheck.net/ and come back when you've fixed what it finds; a good StackOverflow question asks about *one specific* problem (and names that problem in the question!); right now, this is overbroad and has a collection of different unrelated issues. – Charles Duffy Aug 02 '18 at 22:09
  • ...so, `for file in /Name With Spaces/*.txt` is going to first have `file=/Name`, then have `file=With`, then `file=Spaces/*.txt` (or, if you have a directory just named `Spaces`, will iterate over files with names ending in `.txt` it contains. By contrast, `for file in "/Name With Spaces/"*.txt` will behave as intended. But then you need to use `"$file"`, not bare `$file`, to refer to it later. – Charles Duffy Aug 02 '18 at 22:17
  • I used Shellcheck and i have it almost working except for my twarc hydrate line – Chase Aug 02 '18 at 22:36
  • As I said in my very first comment, `$(foo)` needs to be `${foo}` or just `$foo` if you want it to expand a variable by that name. `$(...)` runs a program named `...` and expands to its output, whereas `${...}` expands to the contents of the variable named `...` – Charles Duffy Aug 02 '18 at 22:37
  • If i wanted to do twarc hydrate filet.txt < tweet1.jsonl then file 2, etc... is this the right way? – Chase Aug 02 '18 at 22:38
  • `num=0; for file in *.txt; do twarc hydrate "$file" > "tweets${num}.jsonl"; num=$(( num + 1)); done` is closer. `num + num` doubles your number every time, instead of just adding 1 to it. – Charles Duffy Aug 02 '18 at 22:39
  • waitaminute, your original code was `>`, writing *to* the `.jsonl` files, but your latest comment is `<`, reading *from* the `.jsonl` files. Which of those things do you actually want? – Charles Duffy Aug 02 '18 at 22:40

0 Answers0