I have an issue that I'm not able to solve. I want to run a bash script that is inside my Docker CLI container, and I want to execute it, passing parameters. Usually, I run scripts using a notation like this:
docker exec -i $CLI_ID bash "./script.sh"
But I don't know how to pass parameters to the script. I tried to execute it with:
docker exec -i $CLI_ID bash "./script.sh PARAM"
But it doesn't work. How can I do it?
Thanks
Try with bash -c option
docker exec -i $CLI_ID bash -c "./script.sh PARAM"
Hope this helps.
Make sure the script is executable , then you dont need the bash and double quotes etc. Just run it striaght away by path/name and provide the options.
[root#ap-p1m-ff ~]# docker exec -i 0c cat /tmp/test.sh
#!bin/bash
ls $1
[root#ap-p1m-ff ~]# docker exec -i 0c /tmp/test.sh -l
total 4
-rw-r--r-- 1 root root 159 Jun 4 18:32 RELEASE
drwxr-xr-x 2 root root 120 Jun 4 18:33 assets
drwxr-xr-x 1 root root 31 Jun 4 18:33 bin
drwxr-xr-x 2 root root 6 Apr 12 2016 boot
drwxr-xr-x 5 root root 340 Jun 7 19:50 dev
drwxr-xr-x 1 root root 22 Jun 7 19:50 etc
but if the script is not already executble the bash should work as well:
[root#ap-p1m-ff ~]# docker exec -i 0c bash /tmp/test.sh -l
total 4
-rw-r--r-- 1 root root 159 Jun 4 18:32 RELEASE
drwxr-xr-x 2 root root 120 Jun 4 18:33 assets
drwxr-xr-x 1 root root 31 Jun 4 18:33 bin
drwxr-xr-x 2 root root 6 Apr 12 2016 boot
drwxr-xr-x 5 root root 340 Jun 7 19:50 dev
drwxr-xr-x 1 root root 22 Jun 7 19:50 etc
drwxr-xr-x 2 root root 6 Apr 12 2016 home
drwxr-xr-x 1 root root 45 Sep 13 2015 lib
drwxr-xr-x 2 root root 34 May 15 14:22 lib64
drwxr-xr-x 2 root root 6 May 15 14:22 media
Add your parameter as a separate argument rather than as part of the filename:
docker exec -i "$CLI_ID" bash "./script.sh" PARAM
This way, you don't have to add another level of escaping to the parameter.
Related
I want to run a script with Gitlab CI/CD, I have tested and it work well on local but when I run it on Gitlab, I got the error: ': No such file or directory, this look very strange to me. Here is the output I got from Gitlab
$ ls -la
total 24
drwxrwxrwx 6 root root 189 Dec 17 02:42 .
drwxrwxrwx 4 root root 38 Dec 11 01:45 ..
drwxrwxrwx 7 root root 143 Dec 17 02:42 .git
-rw-rw-rw- 1 root root 867 Dec 17 02:42 .gitlab-ci.yml
-rw-rw-rw- 1 root root 123 Dec 14 02:38 .gitmodules
-rw-rw-rw- 1 root root 1284 Dec 16 02:12 Dockerfile
-rw-rw-rw- 1 root root 173 Dec 17 02:26 README.md
drwxrwxrwx 2 root root 60 Dec 17 02:27 be
drwxrwxrwx 2 root root 35 Dec 16 02:28 fe
-rw-rw-rw- 1 root root 205 Dec 17 02:26 config.json
drwxrwxrwx 2 root root 82 Dec 17 02:42 scripts
-rwxrwxrwx 1 root root 1209 Dec 17 02:42 test
$ ls -la ./test
-rwxrwxrwx 1 root root 1209 Dec 17 02:42 ./test
$ git diff #~1 -- config.json | grep -A4 be | ./test
': No such file or directory
Cleaning up file based variables
00:01
ERROR: Job failed: exit code 127
Note:
The command git diff #~1 -- config.json | grep -A4 be is success and it give a string output
The content of file ./test looks like this:
#!/bin/bash
echo "hello"
What's wrong with it?
When trying to enter into Docker VM in mac, I am getting screen is terminating error whereby I cannot go into Docker Desktop. In Mac, Docker host is not the mac as it runs as a VM. Any solutions?
Thank you for the help.
Workarounds
I think it's a bug since version 2.3.0.4 of Docker Desktop for Mac, as I have used screen successfully to access the Docker VM in the past on Mac without this issue. (edit: this issue appears to be still present at version 2.4.0.0)
There are some alternative methods to access the Docker VM. Here is one that worked for me:
workaround method 1
run this command to enter shell of the Docker VM:
docker run -it --privileged --pid=host debian nsenter -t 1 -m -u -n -i sh
Then you can do what you need to do at the usual location for Docker volumes:
/ # ls -lah /var/lib/docker/volumes/
total 72
drwx------ 10 root root 4.0K Aug 27 11:34 .
drwx--x--x 15 root root 4.0K Oct 31 00:51 ..
drwxr-xr-x 3 root root 4.0K Aug 26 10:44 14ce94e174839f1947efa6fcbf5ac1fb2ea3b0f0b3f25311fee333ee374576b6
drwxr-xr-x 3 root root 4.0K Aug 25 12:34 34eb2bd80931ff954e1da80c5383beb4def61129d4005432b77080531cd10a5a
drwxr-xr-x 3 root root 4.0K Aug 27 11:34 830de2ce31519c921b50c448964b54517ca4461d337b56a9fd6e5b354ace3247
drwxr-xr-x 3 root root 4.0K Aug 25 12:34 data-layer_mongo_data
drwxr-xr-x 3 root root 4.0K Aug 26 11:01 ebb440184703bfad17184bd5ff74b677b50d74b8f0fbdd116506a1fcacfb00cd
drwxr-xr-x 3 root root 4.0K Aug 27 11:27 hawakening-services_back-end_log
drwxr-xr-x 3 root root 4.0K Aug 27 11:27 hawakening-services_front-end_log
drwxr-xr-x 3 root root 4.0K Aug 27 11:27 hawakening-services_mongo_data
-rw------- 1 root root 64.0K Oct 31 00:51 metadata.db
workaround method 2
Here's another one that also worked for me.
Run this command to enter shell of the Docker VM:
docker run --rm -it --privileged --pid=host walkerlee/nsenter -t 1 -m -u -i -n sh
As before, here is where the Docker volumes' directories are:
/ # ls -lah /var/lib/docker/volumes/
total 72
drwx------ 10 root root 4.0K Aug 27 11:34 .
drwx--x--x 15 root root 4.0K Oct 31 00:51 ..
drwxr-xr-x 3 root root 4.0K Aug 26 10:44 14ce94e174839f1947efa6fcbf5ac1fb2ea3b0f0b3f25311fee333ee374576b6
drwxr-xr-x 3 root root 4.0K Aug 25 12:34 34eb2bd80931ff954e1da80c5383beb4def61129d4005432b77080531cd10a5a
drwxr-xr-x 3 root root 4.0K Aug 27 11:34 830de2ce31519c921b50c448964b54517ca4461d337b56a9fd6e5b354ace3247
drwxr-xr-x 3 root root 4.0K Aug 25 12:34 data-layer_mongo_data
drwxr-xr-x 3 root root 4.0K Aug 26 11:01 ebb440184703bfad17184bd5ff74b677b50d74b8f0fbdd116506a1fcacfb00cd
drwxr-xr-x 3 root root 4.0K Aug 27 11:27 hawakening-services_back-end_log
drwxr-xr-x 3 root root 4.0K Aug 27 11:27 hawakening-services_front-end_log
drwxr-xr-x 3 root root 4.0K Aug 27 11:27 hawakening-services_mongo_data
-rw------- 1 root root 64.0K Oct 31 00:51 metadata.db
I was toying with the idea of creating an alias that would allow me to list the contents of a target subdirectory, without changing to that directory.
I have successfully been able to create both an alias and a script to change directory and display contents. I call the script leap and it is simply:
#!/bin/bash
PATH=/bin:/usr/bin:.
# script to change to a directory, then display current contents
cd $1 && ls -l -a
The alias I use to trigger leap is defined: alias lp='. ~/scripts/leap'
My hope was I could simply create an alias named pk (pk is for peek) and concatenate my leap script and a standard bash command using &&. I could not be more wrong.
Current Directory
For reference, here is the contents of my current home directory (my user id has been replaced with $$$$):
drwxr-xr-x 3 $$$$$$ 46374 23 Aug 30 11:40 Fall_2019
drwxr-xr-x 5 $$$$$$ 46374 66 Aug 28 09:01 PAST_COURSES
drwxr-xr-x 3 $$$$$$ 46374 22 Aug 30 12:03 repos
drwxr-xr-x 3 $$$$$$ students 117 Aug 31 09:06 scripts
The Attempt(s)
Using alias pk='lp $1 && cd ..'
Entering [$$$$#host ~]$ pk PAST_COURSES results in:
-rw------- 1 $$$$$$ students 1766 Feb 28 2018 ~
drwx------ 10 $$$$$$ students 4096 Aug 31 09:06 .
drwx--x--x 1232 root root 28672 Aug 30 16:03 ..
-rw------- 1 $$$$$$ students 11368 Aug 30 12:20 .bash_history
-rw------- 1 $$$$$$ students 18 Aug 21 2017 .bash_logout
-rw------- 1 $$$$$$ students 180 Mar 8 2018 .bash_profile
-rw------- 1 $$$$$$ students 526 Aug 30 11:19 .bashrc
-rw------- 1 $$$$$$ students 266 Aug 21 2017 .cshrc
drwxr-xr-x 3 $$$$$$ 46374 23 Aug 30 11:40 Fall_2019
drwxr-xr-x 8 $$$$$$ students 155 Aug 30 12:14 .git
-rw-r--r-- 1 $$$$$$ students 87 Apr 11 2018 .gitconfig
-rw-r--r-- 1 $$$$$$ students 12288 Jan 29 2018 .hello.swp
-rw------- 1 $$$$$$ students 172 Aug 21 2017 .kshrc
-rw------- 1 $$$$$$ students 189 Mar 13 2018 .lesshst
-rw-r--r-- 1 $$$$$$ students 20480 Jan 29 2018 .ls.swn
drwxr-xr-x 5 $$$$$$ 46374 66 Aug 28 09:01 PAST_COURSES
drwxr----- 3 $$$$$$ 46374 18 Aug 30 12:16 .pki
drwxr-xr-x 3 $$$$$$ 46374 22 Aug 30 12:03 repos
drwxr-xr-x 3 $$$$$$ students 117 Aug 31 09:06 scripts
drwx------ 3 $$$$$$ students 103 Aug 30 11:12 .ssh
-rw------- 1 $$$$$$ students 12288 Sep 6 2017 .swp
drwxr-xr-x 2 $$$$$$ 46374 23 Aug 31 09:06 .vim
-rw-r--r-- 1 $$$$$$ students 8129 Aug 31 09:06 .viminfo
-rw-r--r-- 1 $$$$$$ students 142 Feb 14 2018 .vimrc
[$$$$#host home]$
As you can see, this displays the current directory ( ~ ) rather than switching to PAST_COURSES and displaying it. Additionally, the alias jumps up one directory above the current directory ( ~ ) rather than returning to it from PAST_COURSES.
Incidentally, I also get this exact result when I try using the following aliases:
pk='. ~/scripts/leap $1 && cd ..'
(using the script for leap rather than the alias)
pk='cd $1 && ls -l -a && cd ..'
(using the exact code inside leap )
Findings
In my tinkering I have noticed a few things:
First, if I simply type $$$$#host ~]$ ~/scripts/leap *[dir-name]* I actually get EXACTLY what I want - a look into a directory without changing to it. All by omitting the leading ., which boggles me.
Second, I can fix the current pk alias by changing the trailing cd .. to cd $(pwd), though it will not display the target directory instead of the current one.
At this point, I'd like a little help - not just in a script or alias that will do the job. An explanation that explains this behavior that I'm seeing would be marvelous.
Don't use . (the source builtin)
alias pk='. ~/scripts/leap $1 && cd ..' (using the script for leap rather than the alias)
is not equivalent to
alias pk='cd $1 && ls -l -a && cd ..'
In the first one, the . builtin (also known as source) is used, while in the second it is not. . doesn't just execute a command, it executes it in the current shell context. From the documentation:
. (a period)
. filename [arguments]
Read and execute commands from the filename argument in the current shell context.
That means anything the script does effects your current shell context. If the script changes directories, so does your current context.
If, on the other hand, the first version omitted the . like this:
alias pk='~/scripts/leap $1 && cd ..'
then the contents of the leap script would run in it's own bash context, but your current context would move up one directory (since the cd .. isn't inside the leap script).
Additional Recommendation on Functions vs Aliases
You could implement pk using a function like this:
pk() {
pushd $1
if [[ $? == 0 ]]; then
# Successfully changed directories.
# Run command
$2
# Return from the pre-pushd directory.
popd
fi
}
From the Bash Manual | 6.6 Aliases:
For almost every purpose, shell functions are preferred over aliases.
Example Alias
alias foo="echo bar"
Equivalent Function
foo() {
echo bar
}
I have a bash script which acts as a post-process script for utorrent-server that passes on variables to a media renamed called FileBot.
Script:
#!/bin/bash
TORRENT_NAME=$1
TORRENT_PATH=$2
TORRENT_LABEL=$3
TORRENT_KIND=$4
TORRENT_TITLE=$5
/usr/share/filebot/bin/filebot.sh -script fn:amc --output "/mnt/Storage/" \
--log-file "amc.log" --action move --conflict override -non-strict \
--def music=n subtitles=en artwork=n xbmc="192.168.0.123" deleteAfterExtract=y \
clean=y "ut_dir=$TORRENT_PATH" "ut_file=$TORRENT_NAME" "ut_kind=$TORRENT_KIND" \
"ut_title=$TORRENT_TITLE" "ut_label=$TORRENT_LABEL" "ut_state=5" "seriesFormat=TV \
Shows/{n}/Season {s.pad(2)}/{n} - {s00e00} - {t}" "movieFormat=Movies/{n} ({y})/{n} ({y})" \
&>> /home/xbmc/run.log
If i run this script manually, it works as intended, however when uTorrent executes it, it returns "No such file or directory." via stderr. I originally had uTorrent calling this script directly however I was having the same issue.
Does anyone know what could cause this?
UPDATE (Permissions for all directories/folders):
drwxr-xr-x 3 root root 4096 Nov 27 23:52 /home
drwxr-xr-x 20 xbmc xbmc 4096 Dec 15 21:46 /home/xbmc
drwxr-xr-x 10 root root 4096 Oct 17 06:51 /usr
drwxr-xr-x 218 root root 4096 Dec 13 15:32 /usr/share
drwxr-xr-x 3 root root 4096 Dec 15 15:55 /usr/share/filebot
drwxr-xr-x 2 root root 4096 Dec 15 18:56 /usr/share/filebot/bin
-rwxr-xr-x 1 xbmc xbmc 615 Dec 15 21:44 /home/xbmc/run.sh
-rwxr-xr-x 1 root root 552 Dec 15 18:56 /usr/share/filebot/bin/filebot.sh
Change the current working directory.
IF filebot.sh is the no-such-file, I suggest you to try with this:
chmod -R a+x /usr/share/filebot/bin/filebot.sh
IF it is your run.sh,
chmod -R a+x /home/xbmc/run.sh
You can try running filebot.sh as the owner. I think it worths a shot.
chown YOURUSERNAME /usr/share/filebot/bin/filebot.sh
chmod u+s /usr/share/filebot/bin/filebot.sh
I have installed the global npm package jslint and it lives here
$ ls -la /usr/local/bin/jslint
lrwxr-xr-x 1 lust admin 40 Feb 12 15:31 /usr/local/bin/jslint -> ../lib/node_modules/jslint/bin/jslint.js
$ ls -la /usr/local/lib/node_modules/jslint/bin
total 8
drwxr-xr-x 3 lust staff 102 Apr 16 2012 .
drwxr-xr-x 10 lust staff 340 Feb 12 15:31 ..
-rwxr-xr-x 1 lust staff 2330 Apr 16 2012 jslint.js
$ which jslint
/usr/local/bin/jslint
$ head -3 /usr/local/bin/jslint
#!/usr/bin/env node
var linter = require("../lib/linter");
So it is without any doubt whatsoever at this point that jslint is in fact being run from this dir and not here:
$ ls -la .npm/jslint/0.1.9/package/bin/
total 8
drwxr-xr-x 3 lust staff 102 Apr 16 2012 .
drwxr-xr-x 9 lust staff 306 Feb 12 15:31 ..
-rwxr-xr-x 1 lust staff 2330 Apr 16 2012 jslint.js
There appear to be two copies of the package, one in /usr/local/ and one in ~/.npm. Why is there one in .npm and is it safe for me to remove it?
~/.npm is a cache that npm uses to avoid re-downloading the same package multiple times. There's no harm in removing it. You can empty it with the command:
npm cache clean