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?