linux - Running bash script outside of directory
2014-07
I am trying to run a script that's in a directory I have admin rights for, but I am trying to run it from somewhere that is not the directory.. I.E. its a generate load in script that reads all the files in the directory and it gives an output based on those files. But when I try to call it from the terminal it doesn't run unless active directory is the directory where the script is located, is this a permissions issue?
I have tried running it this way:
sh /path/to/file/FILE
./path/to/file/FILE
but when I cd
to directory and run
./FILE
it runs fine.
Permissions for my account:
drwxrwxr-x 5 edennis edennis 4096 Nov 14 14:35 .
Permissions for script:
-rwxrwxr-x 1 edennis edennis 3644 Nov 14 11:45 zScript
Permissions for files script is accessing:
-rw-rw-r-- 1 edennis edennis 10437424 Oct 15 10:27 document.txt
Contents of script:
#!/bin/bash
for file in *.txt
do
echo "this is my favorite file " "$file" >> output.txt;
done
The problem is that you still remain in the current directory when you run the script. So, the line
for file in *.txt
will expand the glob (*.txt
) to all text files in your current directory. Of course, if your current directory (not the script's directory!) doesn't have any text files, you won't get any output.
If you want to change to the script's directory from within the script (which would make the *.txt
glob work), see this Stack Overflow post: Can a Bash script tell what directory it's stored in?
While we're at it – don't forget to quote "$file"
properly when you use to prevent a filename with spaces from breaking your commands. It doesn't matter in your case because echo
doesn't care about the arguments, but it's a good habit to develop.
Hey All, wrote this script to confuse my girlfriend when she is using my computer. The only trouble is that if the terminal gets closed the script stops to execute. It currently executes in the background but how can I make it run in the background but not in a terminal?
(sleep 5; say "Hello Girlfriend")&
You could schedule it to run at a specific time with at
or cron
in the background.
Have you tried nohup
?
nohup command >/dev/null 2>&1 &
I promise that no matter how much she likes you, 5 seconds is too fast an interval. Really.
nohup
(and in bash disown
) will allow you to detach the process from the initiating terminal.
You can suspend execution by Ctrl-Z and then let it run in the background with bg ( background ).
If you want to bring it forth then fg or %.
use at
or cron
as john suggested, and then use wall
to send her a message to her terminal.