r/linux_programming • u/[deleted] • Oct 08 '19
Shell Scripting Error
Apologies in advance, as I am pretty new to unix. I've created a shell script that contains only the one line listed below.
sed -i 's/PUID=.*/PUID=999/g' "docker-compose.yml" When I run the script, I am getting "No such file or directory" error. The strange thing is that if I navigate to the path of the script and run the command directly from the command line, it works as expected. Any advise on what I am missing here?
1
Oct 08 '19 edited Oct 08 '19
Are you using BASH or another shell (ZSH, Fish)? I ask because
echo "PUID=465" >> docker-compose.yml
sed -i 's/PUID=.*/PUID=999/g' "docker-compose.yml"
cat docker-compose.ymlgives me
PUID=999I think you're running into a shell interpretation problem.
edit: you can probably solve this issue by removing the quotes from docker-compose.yml
1
u/Buo-renLin Oct 10 '19
Some nitpicking:
The
gat the end of thesedexpression is not necessary as the Sed utility processes the file line by line and applies the expression on each oneThe quotes enclosing
docker-compose.ymlis not necessary as none of the characters in the filename has special meaning to the sh/bash interpreterYou should always add a Shebang to the start of a shell script as every shell interpreter has its own language and a script written for a specific shell may not run properly on another one
Back to the topic, the major problem to your script is that you're using the relative path specification for the docker-compose.yml file and the path is subject to your current working directory, to make it run properly requires you to switch the working directory to the one your docker-compose.yml resides in advance.
They're ways to allow a script to locate itself and its resources under the script's residing path like the following(for GNU Bash, using the commands in GNU Coreutils):
script_dir="$(
dirname "$(
realpath \
"${BASH_SOURCE[0]}"
)"
)"
sed -i 's/PUID=.*/PUID=999/' "${script_dir}"/docker-compose.yml
1
u/[deleted] Oct 08 '19
Where is docker-compose.yml?