1

I'm completely new to this, and practicing as much as possible. I'm trying to create a variable for a path created in the code that I also want to access. I keep getting 'ambiguous redirect'. the code looks like

#!/bin/bash

echo "Type 1 to create log1. Type 2 to create log2."

read num

if [ "$num" == 1 ]
then
   touch /home/user/Documents/log1.txt

   log1location=/home/user/Documents/log1.txt

   echo "log1.txt created"    
fi

if [ "$num" == 2 ]
then    
   touch /home/user/Documents/log2.txt

   log2location=/home/user/Documents/log2.txt

   echo "log2.txt created"    
fi

when I execute the code using either number, then try to echo into the path like:

echo "Hello" >> $log2location

i get

bash: $log2location: ambiguous redirect

but if i manually type in log2location=/home/user/Documents/log2.txt, and retry the echo, it works.

I'm assuming the variable isn't being created when executing the script. I verified that the txt file is being created.

edit I saved the code as a .sh file. I then used chmod +x to make it executable. I then execute the script using ./

I figured out that I need to use . ./script.sh to get the variables into current shell. It now works the way I want it to.

I also exported the directory in which my script is stored to PATH via ~/.bashrc

export PATH="$HOME/Documents:$PATH"

I know that this isn't a great idea. I'm using a VM to practice.

Then I typed alias logfile=/home/Documents/nameofscript.sh

I rebooted, and can now use logfile to execute the script from wherever. BUT i'm getting the original problem where the variables lead to 'ambiguous redirect'. How do I add source command to make the variables in current shell?

dvines
  • 51
  • 6
  • 2
    If you are *executing* the file rather than sourcing it, `log2location` is only defined in the process that executes the script, not in the shell from which you initiated the script. – chepner Jul 05 '22 at 18:14
  • BTW -- (1) _please_ indent your code for the sake of your readers' sanity; (2) include enough details to reproduce the problem -- right now, the distinction between `./yourscript`, `sh yourscript`, `bash yourscript` or `. yourscript` / `source yourscript` is critical, but you aren't telling us _how_ you run your script, so we don't know which one you're using. – Charles Duffy Jul 05 '22 at 18:18
  • Another aside: `[` only guarantees `=` as a string comparison operator. `==` is not part of the POSIX standard for `test`, so using it makes your code unnecessarily fragile (if it's run with baseline `sh`, the `==` operator can result in an error). See https://pubs.opengroup.org/onlinepubs/9699919799/utilities/test.html – Charles Duffy Jul 05 '22 at 18:19
  • I saved the code as a .sh file. I then used chmod +x to make it executable. I then execute the script using ./ – dvines Jul 05 '22 at 18:36
  • Okay, that explains the problem (and confirms that the duplicate it's closed with is appropriate). You're running the script as a separate process; it can only set variables in its own shell interpreter, not in the separate interactive shell it was started from. – Charles Duffy Jul 05 '22 at 19:38
  • If you want a variable to persist into a subprocess, you need to export it as an environment variable – xpusostomos Jul 06 '22 at 03:23

0 Answers0