Why a * does not get replaced in a variable value - bash

I tried to write a generic Dockerfile and I would like not changed the file names each time the version changes. How is it possible to use a * for variable names?
ADD ./Trimmomatic-*.zip /tmp/
RUN cd /usr/local ; unzip /tmp/Trimmomatic*.zip
ENV JAR_LOC echo `ls -1 /usr/local/Trimmomatic/trimmomatic-*.jar`
RUN chmod 755 ${JAR_LOC}
RUN ln -s /usr/local/Trimmomatic/trimmomatic-*.jar /usr/local/bin/trimmomatic.jar
RUN rm -rf /tmp/Trimmomatic*.zip
Unfortunately, I got the following error:
Step 12 : ADD ./Trimmomatic-*.zip /tmp/
---> Using cache
---> b0104788f151
Step 13 : RUN cd /usr/local ; unzip /tmp/Trimmomatic*.zip
---> Using cache
---> ffeae9bbd5f6
Step 14 : ENV JAR_LOC echo `ls -1 /usr/local/Trimmomatic/trimmomatic-*.jar`
---> Using cache
---> e4f836c140ca
Step 15 : RUN chmod 755 ${JAR_LOC}
---> Running in 539728a7f13f
chmod: cannot access '755': No such file or directory
chmod: cannot access 'echo': No such file or directory
chmod: cannot access '`ls': No such file or directory
chmod: cannot access '/usr/local/Trimmomatic/trimmomatic-*.jar`': No such file or directory
The command '/bin/sh -c chmod 755 ${JAR_LOC}' returned a non-zero code: 1
Why a * does not get replaced in variable value

Related

File won't copy to Docker file?

I run this file (partial) under "root" account on my Synology.
but it won't start, in my error log I got this message:
[FATAL tini (9)] exec /home/qgis/cmd.sh failed: No such file or directory
Can someone help me please...
This is the part where cmd.sh is added:
ENV TINI_VERSION v0.19.0
ADD https://github.com/krallin/tini/releases/download/${TINI_VERSION}/tini /tini
RUN chmod +x /tini
ENV QGIS_PREFIX_PATH /usr
ENV QGIS_SERVER_LOG_STDERR 1
ENV QGIS_SERVER_LOG_LEVEL 2
COPY cmd.sh /home/qgis/cmd.sh
RUN chmod -R 777 /home/qgis/cmd.sh
RUN chown qgis:qgis /home/qgis/cmd.sh
USER qgis
WORKDIR /home/qgis
ENTRYPOINT ["/tini", "--"]
CMD ["/home/qgis/cmd.sh"]
create a file cmd.sh with this content:
This is the cmd.sh file:
#!/bin/bash
[[ $DEBUG == "1" ]] && env
exec /usr/bin/xvfb-run --auto-servernum --server-num=1 /usr/bin/spawn-fcgi -p 5555 -n -d /home/qgis -- /usr/lib/cgi-bin/qgis_mapserv.fcgi

Docker container unable to ignore the EntryPoint bash script failure

Bash script:
clonePath=/data/config/
git branch -r | fgrep -v 'origin/HEAD' | sed 's| origin/|git checkout |' > checkoutAllBranches.sh
chmod +x checkoutAllBranches.sh
echo "Fetch branch: `cat checkoutAllBranches.sh`"
./checkoutAllBranches.sh
git checkout master
git remote rm origin
rm checkoutAllBranches.sh
for config_dir in `ls -a`; do
cp -r $config_dir $clonePath/;
done
echo "API Config update complete..."
Dockerfile which issues this script execution
ENTRYPOINT ["sh","config-update-force.sh","|| true"]
The error below causes the container startup failure despite setting the command status to 0 manually using || true
ERROR:
Error:
cp: cannot create regular file '/data/./.git/objects/pack/pack-27a9d...fb5e368e4cf.pack': Permission denied
cp: cannot create regular file '/data/./.git/objects/pack/pack-27a9d...fbae25e368e4cf.idx': Permission denied
I am looking for 2 options here:
Change these file permissions and then store them in the remote with rwx permissions
Do something to the docker file to ignore this script failure error and start the container.
DOCKERFILE:
FROM docker.hub.com/java11-temurin:latest
USER root
RUN apt-get update
RUN apt-get -y upgrade
RUN apt-get install -y rsync telnet vim wget git
RUN mkdir -p /opt/config/clone/data
RUN chown -R 1001:1001 /opt/config
USER 1001
ADD build/libs/my-api-config-server.jar .
ADD config-update-force.sh .
USER root
RUN chmod +x config-update-force.sh
USER 1001
EXPOSE 8080
CMD java $BASE_JAVA_OPTS $JAVA_OPTS -jar my-api-config-server.jar
ENTRYPOINT ["sh","config-update-force.sh","|| true"]
BASH SCRIPT:
#!/bin/bash
set +e
set +x
clonePath=/opt/clone/data/data
#source Optumfile.properties
echo "properties loaded: example ${git_host}"
if [ -d my-api-config ]; then
rm -rf my-api-config;
echo "existing my-api-config dir deleted..."
fi
git_url=https://github.com/my-api-config-server
git clone https://github.com/my-api-config-server
cd my-api-config-server
git branch -r | fgrep -v 'origin/HEAD' | sed 's| origin/|git checkout |' > checkoutAllBranches.sh
chmod +x checkoutAllBranches.sh
echo "Fetch branch: `cat checkoutAllBranches.sh`"
./checkoutAllBranches.sh
git checkout master
git remote rm origin
rm checkoutAllBranches.sh
for config_dir in `ls -a`; do
cp -r $config_dir $clonePath/;
done
echo "My API Config update complete..."
When you do in the script...
chmod +x checkoutAllBranches.sh
...than why not before cp
chmod -R +rwx ${clonePath}
...or if the stderr message 'wont impact anything'...
cp -r $config_dir $clonePath/ 2>/dev/null;
...even cp dont copy -verbosly.
?
When your Dockerfile declares an ENTRYPOINT, that command is the only thing the container does. If it also declares a CMD, the CMD is passed as additional arguments to the ENTRYPOINT; it is not run on its own unless the ENTRYPOINT makes sure to execute it.
Shell errors are not normally fatal, and especially if you explicitly set +e, even if a shell command fails the shell script will keep running. You see this in your output where you get multiple cp errors; the first error does not terminate the script.
You need to do two things here. The first is to set the ENTRYPOINT to actually run the CMD; the simplest and most common way to do this is to end the script with
exec "$#"
The second is to remove the || true from the Dockerfile. As you have it written out currently, this is passed as the first argument to the entrypoint wrapper – it is not run through a shell and it is not interpreted as a "or" operator. If your script begins with a "shebang" line and is marked executable (both of these are correct in the question) the you do not explicitly need the sh interpreter.
# must be a JSON array; no additional "|| true" argument; no sh -c wrapper
ENTRYPOINT ["./config-update-force.sh"]
# any valid CMD will work with `exec "$#"
CMD java $BASE_JAVA_OPTS $JAVA_OPTS -jar my-api-config-server.jar

Problem of RUN shell script in Dockerfile in Mac OS X

I'm trying to use Docker with my macOS Catalina v10.15.4
When I tried to RUN ./test.sh in Dockerfile and there is some error occured.
This is my Dockerfile:
FROM ubuntu:18.04
RUN mkdir -p /home/rootfs/src
COPY test.sh /home/rootfs
WORKDIR /home/rootfs
RUN chmod +x test.sh && ./test.sh
When I tried to build this, it did not find the script as below:
$ docker build -t test .
Sending build context to Docker daemon 264.6MB
Step 1/5 : FROM ubuntu:18.04
---> 4e5021d210f6
Step 2/5 : RUN mkdir -p /home/rootfs/src
---> Running in d0813632475d
Removing intermediate container d0813632475d
---> 87a6e284993d
Step 3/5 : COPY test.sh /home/rootfs
---> 26d281d0002c
Step 4/5 : WORKDIR /home/rootfs
---> Running in 4c7e81a514a7
Removing intermediate container 4c7e81a514a7
---> dab2872eb15a
Step 5/5 : RUN chmod +x test.sh && ./test.sh
---> Running in a03f3e5d29b4
/bin/sh: 1: ./test.sh: not found
The command '/bin/sh -c chmod +x test.sh && ./test.sh' returned a non-zero code: 127
I check the info of test.sh file test.sh in macOS.
Got this:
test.sh: POSIX shell script text executable, ASCII text, with CRLF line terminators
Your test.sh script contains Windows line endings as specified in output of file test.sh. Convert them to Unix line endings:
$ dos2unix test.sh
dos2unix: converting file test.sh to Unix format...

mkdir always creates a file instead a directory

First I want to say that I don't really know what I should look for, here in Stack Overflow and what could be a good query for my problem.
In simple words I want to create a new directory and than do some file operations in it. But with the script that I have crafted I got always a file instead of a directory. It seems to be absolutely regardless how I stick the code together there is always the same result. I hope tat masses can help me with their knowledge.
Here is the script:
#!/bin/bash
DLURL=http://drubuntu.googlecode.com/git'
d7dir=/var/www/d7/'
dfsettings=/var/www/d7/sites/default/default.settings.php
settings=/var/www/d7/sites/default/settings.php
#settiing up drush
drush -y dl drush --destination=/usr/share;
#Download and set up drupal
cd /var/www/;
drush -y dl drupal;
mkdir "$d7dir"; #this is the line that always produces a file instead a directory
# regardless if it is replaced by the variable or entered as
# /var/www/d7
cd /var/www/drup*;
cp .htaccess .gitignore "$d7dir";
cp -r * "$d7dir";
cd "$d7dir";
rm -r /var/www/drup*;
mkdir "$d7dir"sites/default/files;
chmod 777 "$d7dir"sites/default/files;
cp "$dfsettings" "$settings";
chmod 777 "$settings";
chown $username:www-data /var/www/d7/.htaccess;
wget -O $d7dir"setupsite $DLURL/scripts/setupsite.sh; > /dev/null 2>&1
chmod +x /var/www/setupsite;
echo "Login Details following...";
read -sn 1 -p "Press any key to continue...";
bash "$d7dir"setupsite;
chown -Rh $username:www-data /var/www;
chmod 644 $d7dir".htaccess;
chmod 644"$settings";
chmod 644"$dfsettings";
exit
I hope someone got the reason for that.
There are many way to debug a shell-scripting.
Add set -x in your beginning script
Get the return value.
mkdir 'the-directory'
ret=$?
if test $ret -eq 0; then
echo 'Create success.'
else
echo 'Failed to create.'
fi
Set to verbose mode $ mkdir -v 'the-directory'
Try this command $ type mkdir, to checking mkdir command.

Redirecting a bash error

I am writing a bash script, which has a problem:
path=$(pwd)
data=$(ls -al $path) > /dev/null 2>/dev/null
The problem occurs if $path is a "locked" directory (no permission for user x), call it "BadDir". In that case, the program outputs:
ls: cannot access /home/user/.../BadDir/..: Permission denied
All I want is to hide this output.
I know there is redirection to /dev/null but I don't know how to use it in this particular case.
you can redirect all error message to another with using EXEC
for test, first create folder
mkdir /tmp/t/
sudo chown root /tmp/t/
sudo chgrp root /tmp/t/
sudo chmod 400 /tmp/t/
e.g:
ls -al /tmp/t/
output:
ls: cannot open directory /tmp/t/: Permission denied
and using EXEC first of file:
exec 2>/dev/null
ls -al /tmp/t/
with exec you can control and redirect all error message or another output

Resources