If I putty into home assistant then I can run each one of these lines on its own and it deletes the files fine and when I run the cp command at the prompt it copies the file from the Wiser_Schedule_Backup directory into the main directory and renames it.
However if I try to run “bash script.sh” it does work but just returns the prompt
I have also tried a chmod 775 script.sh
What I have noticed though is that the equivalent number of files appear in teh config directory but consist of random letters (e.g. CG3BBD~O) and the contents are the contents of the files I am trying to copy. Feels like it is some kind of permissions issue.
Once I can get this running as a script file I will then try to add it to an automation but even at this basic level I am struggling to delete and copy files.
What am I doing wrong?
What do you mean by saying it just returns the prompt?
Your shell script should not print anything if everything works, so the only thing you’ll see is your shell prompt after the script finishes running.
cp seems to have an option that creates so-called backup files (see man cp), but it should not be enabled by default.
If there were any permission issues, rm and cp would complain (write stuff to stderr).
Try adding the following command between the last rm and first cp. It will wait for you to press the return key. While it is waiting, open another ssh session and verify that the files are in fact gone.
read -p "Done removing. "
You’ll probably need to configure sudo to not ask for your passwords if you want to run this script automatically. Do you actually need sudo? I think HA is running as root in the docker container.
EDIT: Please clarify where you are running the script. Your HA says Docker: true, but the default container does not seem to have sudo at all. Are you running this on your host OS that just happens to have the home assistant configuration directory in /config? If so, don’t expect everything to work exactly the same after you configure HA to start the script (it will be running inside the docker container).
I use some rm shell commands and some rm commands with find and I do not have sudo in any of them (HA Supervised)… also what’s up with the quotes? Can you rename them to something without the spaces and stuff and try without the quotes? I thought cp was just source path destination path separated with a space (although I am no Linux expert)
@ondras12345 When I said just returns the prompt then what I actually meant to say was didnt return any errors but doesnt delete the files. (Yes I realise it would be a ‘silent’ script.
Agreed I dont think that these are backup files as their content is the same content of the xxx - copy.yaml file rather than the xxx.yaml file I am trying to delete.
I only put sudo in as it was a suggestion from another thread I had read. Have removed that.
Inserted
read -p "Done removing. "
And checked and it hasnt deleted the files. (Do I have the correct path in there - is the rm relative to where the script is namely config/shel/script.sh) have tried rm /config/blue.yaml and also …/blue.yaml
I have Home assistant running on a raspberry pi4 with supervisor. I am running the script from a putty window into the raspberry pi
Basically what I am trying to do via an automation is to delete files in my config directory on homeassistant and then copy files from a subdir back into the top level directory.
Perhaps all of the above is the wrong way about it? Happy to take any alternative suggestions.
Paths that begin with / are absolute paths.
Please show me the output of these commands:
# cd into the correct directory
whoami
pwd
ls -la
cat script.sh
# run your script:
./script.sh # hit ctrl+c when you get to that read -p "Done removing. " to terminate the script
ls -la
The rm did not delete anything because there is no file named config/blue.yaml in your working directory; there is blue.yaml (full path /root/config/blue.yaml).
You seem to be in /root/config/, but then copy files to /config/. Which of these is the correct path?
I would avoid messing with relative paths and specify an absolute path to every file you are trying to remove – that would be either rm /config/blue.yaml or rm /root/config/blue.yaml, depending on which of the directories contains the files you are trying to remove.
Are you sure replacing configuration files is the best way to achieve what you are ultimately trying to do?
BTW to replace a file, you don’t need to delete it first. cp should replace it automatically if you don’t give it any flags (such as -i).
The script does not work even when run manually, so I think we should solve that first.
If it was working, the next step would indeed be to run the following commands and specify the full path to cp and rm.
I know, but when we start the script manually from a shell, the script inherits the shell’s environment. And it still does not work properly. We did not get to starting the script from HA yet. If we were having issues with PATH at this point, we would be seeing “command not found” errors.
Yes, and the new shell should inherit the PATH that was exported in the shell that invoked it.
$ ed
a
#!/bin/sh
echo $test
.
w test
21
q
$ chmod +x test
$ ./test
$ export test="ABC"
$ ./test
ABC
$
As long as the script was being executed from a login shell, it had the correct PATH.
Now, when the script is supposedly working fine when started manually in any working directory, but does not work when HA starts it, I am suspecting PATH could be the issue. Even more so because we cannot see the error messages and are getting return code 127, which is what bash exits with when it cannot find a command.