0

I'm trying to run the following code on files that I choose and put into a variable source file. This is for ease of the user when I export it out. This is the code before trying to add a source file:

for file in ~/Twitter/Users/New/*; do
    [ -f "$file" ] && sed '1,7d' "$file" | head -n -9 > ~/Twitter/Users/TBA/"${file##*/}"
done

So I tried adding a source file like so:

#!/bin/bash
source ~/MYBASHSCRIPTS/Tests/scriptsettings.in
for file in $loctorem/*; do
        [ -f "$file" ] && sed '1,7d' "$file" | head -n -9 > $locdone
done
echo $loctorem
echo $locdone

with the scriptsettings.in configured as such:

loctorem="~/Twitter/Users/New"
locdone='~/Twitter/Users/TBA/"${file##*/}"'

I have tried both half old/half new code but neither work. does it really need to be hard coded in order to run? This will throw my whole "noob friendly" idea in the trash if so...

EDIT--- I only echo it at the end so that I can verify that it is calling the correct locations.

EDIT2--- Here is the exact script I original ran.

#!/bin/bash
for file in ~/Anon/Twitter/OpISIS/New/*; do
        [ -f "$file" ] && sed '1,7d' "$file" | head -n -9 > ~/Anon/Twitter/OpISIS/TBA/"${file##*/}"
done

And the new variant:

source ~/MYBASHSCRIPTS/Tests/scriptsettings.in
for file in $loctorem/*; do
        [ -f "$file" ] && sed '1,7d' "$file" | head -n -9 > "$(locdone_path "$file")"
done

with the source file being:

loctorem=/home/matrix/Anon/Twitter/OpISIS/New
locdone_path() { printf '%s\n' ~/Twitter/Users/TBA/"${1##*/}

as I said before, I'm still pretty new so sorry ifI'm doing an insanely stupid thing in here.. I'm trying to make the input and output folder/file set to a variable that the user can change. in the end this script will be ~80 lines and I want anyone to be able to run it instead of forcing everyone to have directories/files set up like mine. Then I'll have a setup script that makes the file with the variables stored in them so there is a one time setup, or the user can later change locations but they dont have to go into the entire code and change everything just to fit their system.

Matt
  • 107
  • 1
  • 2
  • 10

1 Answers1

1

You've got two problems here. The first are the quotes, which prevent tilde expansion:

# this stores a path with a literal ~ character
loctorem='~/Twitter/Users/New'

# this works
loctorem=~/Twitter/Users/New

# this works too
loctorem="$HOME/Twitter/Users/New"

The second issue is that you're depending on $file before it's available. If you want to store code (an algorithm on how to calculate something, for instance), in your configuration, define a function:

# this can be put in your sourced file
locdone_path() { printf '%s\n' ~/Twitter/Users/TBA/"${1##*/}"; }

...and, later, to use that code, invoke the function:

... | head -n 9 >"$(locdone_path "$file")"

However, if you only want to make the directory customizable, you might do something much simpler:

loctorem=~/Twitter/Users/New
locdone=~/Twitter/Users/TBA

and:

... | head -n 9 >"$locdone/${file##*/}"
Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
  • Now I get this error `line 4: $locdone_path: ambiguous redirect` ? sorry I'm still new to bash. (this was for the old answer, trying the new answer) – Matt May 18 '16 at 17:06
  • Could you show the exact code you used? I can't think of a way what I provided here could cause that particular error. (There was a different bug in my proposed function definition, which I've corrected, but it wouldn't cause an ambiguous redirect). – Charles Duffy May 18 '16 at 17:08
  • Added the additional info to the bottom – Matt May 18 '16 at 17:20
  • I'm sorry -- the info I was asking for was about the *new* script, ie. the details of exactly how you applied the given answer. – Charles Duffy May 18 '16 at 17:21
  • ...I mean, if there's a `$locdone_path` anywhere in your code, you're not following the syntax I gave. – Charles Duffy May 18 '16 at 17:21
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/112316/discussion-between-charles-duffy-and-matt). – Charles Duffy May 18 '16 at 17:37