B
B
bychok3002017-03-14 16:16:41
Programming
bychok300, 2017-03-14 16:16:41

How to fix shell script?

I'm trying to create a log with the current date and run a python script in this log
, I can't figure out if I'm calling the _file variable correctly?

#!/bin/sh

_now=$(date +"%d-%m-%Y-%H-%m")
_file="/home/folder/logs/log_$_now.txt"
sudo touch $_file

cd /home/folder/logs
sudo python3 handleMouseEvent.py > ${_file}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dmitry, 2017-03-14
@bychok300

$ ls -ld /home/folder/logs
will most likely return something like:
give write permissions to the folder to all users:
and throw out sudo from the script:

#!/bin/sh

NOW=$(date +"%d-%m-%Y-%H-%m")
PYFILE="/home/folder/logs/handleMouseEvent.py"
LOGFILE="/home/folder/logs/log_$NOW.txt"

python3 $PYFILE > $LOGFILE

should work.
PS changedir is also better not to use

S
Saboteur, 2017-03-14
@saboteur_kiev

Why do you need sudo to create a file and run python?
If you need sudo - run the whole script, that is:

#!/bin/sh

OUTFILE="/home/folder/logs/log_$(date +"%d-%m-%Y-%H-%m").txt"
cd /home/folder/logs
python3 handleMouseEvent.py > ${OUTFILE}

and then
sudo myscript.sh
Second, you do a redirect > and before that you do a touch - why? redirect and so will create the file you need. The main thing is that the user from which you run (with sudo - root) has write permissions in the specified folder.
PS In the shell, it is customary to name variables in upper case.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question