docker command not found - bash script - bash

I've bash script to load docker images once the application is installed (postinstall script).
Using command manually on the terminal is working, but from postinstall script getting:
docker: command not found
Tried, executing docker with absolute path (which docker) adding the path variable of docker (execute PATH=\usr\bin\docker:$PATH)
Is there something wrong with my macOS or script is wrong.
Original script:
I m checking the macOS chip and then using command docker info trying if docker is installed if its installed loading the images from the working directory otherwise installing docker first and then loading images.
#!/bin/bash
#
#
#
CheckVar=`which docker`
echo "$CheckVar"
if [[ `sysctl machdep.cpu.brand_string` == *"Intel"* ]]
then
if [[ `$CheckVar info` == *"Client"* ]];
then
echo "Mac Chip is Intel"
echo "Docker found!"
cd $Home/$DSTROOT/App
echo "Redirected to working dir.."
sudo `$CheckVar load -i app.tar`
echo "Success"
else
echo "Mac Chip is Intel"
echo "Docker not found installing"
cd $Home/$DSTROOT/App
echo "Redirected to working dir.."
sudo hdiutil attach IntelDocker.dmg
sudo /Volumes/Docker/Docker.app/Contents/MacOS/install
sudo hdiutil detach /Volumes/Docker
sudo `$CheckVar load -i app.tar`
echo "Success"
fi
else
if [[ `$CheckVar info` == *"Client"* ]];
then
echo "Mac Chip is AppleSilicon"
echo "Docker found!"
cd $Home/$DSTROOT/App
echo "Redirected to working dir.."
sudo `$CheckVar load -i app.tar`
echo "Success"
else
echo "Mac Chip is AppleSilicon"
echo "Docker not found installing"
cd $Home/$DSTROOT/App
echo "Redirected to working dir.."
sudo hdiutil attach IntelDocker.dmg
sudo /Volumes/Docker/Docker.app/Contents/MacOS/install
sudo hdiutil detach /Volumes/Docker
sudo `$CheckVar load -i app.tar`
echo "Success"
fi
fi
Any solutions? been trying to solve this issue past 3 days.

First make sure your PATH is valid. It should contain the directory which the docker executable is stored in. Not the absolute path to the executable. Also, execute PATH is not how you set the value. You use export
export PATH=/usr/bin:$PATH
Change your tests from:
[ `$CheckVar info` == *"Client"* ]] to [ "$CheckVar" ]
from man test:
-n STRING
the length of STRING is nonzero
STRING equivalent to -n STRING
The CheckVar variable is empty in the case where docker wasn't found on the system. Reset it in your else statements after installing docker.
sudo /Volumes/Docker/Docker.app/Contents/MacOS/install
sudo hdiutil detach /Volumes/Docker
CheckVar=`which docker`
sudo $CheckVar load -i app.tar

Related

How Do I Fix syntax error: unexpected end of file

my code is
#!/bin/sh
clear
if [[ "$1" == "donut" ]]; then
mkdir ~/.deb
wget ``https://github.com/packageash/repo/blob/main/Donut.deb?raw=true ~/.deb
dpkg --install ~/.deb/Donut.deb
rm ~/.deb/Donut.deb
elif [[ "$1" == "nodonut" ]]; then
sudo rm /usr/local/bin/donut
else
echo "Nothing Picked To Install."
so how do i fix it and can i if i just try cause im trying to make a package manager in shell
Well, if you are looking at only for two options and since this script is really simple, I would recommend changing the if to switch case, because the []s can have different behaviors depending if you are using bash/sh/zsh. I've created a simple snippet with that.
#!/bin/bash
DEB_FOLDER="${HOME}/.deb"
case $1 in
donut)
mkdir -p ${DEB_FOLDER} #try to create folder if it doesn't exists
wget https://cdn.glitch.me/ec7fa70f-caec-4ba9-877f-3f809b43f7ea%2FDonut.deb -O ${DEB_FOLDER}/Donut.deb #save the wget downloaded file to ~/.deb/Donut.deb
sudo dpkg --install ${DEB_FOLDER}/Donut.deb #install the deb package"
rm -f ${DEB_FOLDER}/Donut.deb #removes the ~/.deb/Donut.deb file"
;;
nodonut)
sudo rm /usr/local/bin/donut
;;
*)
echo -n "Nothing Picked To Install"
;;
esac

Is it possible to access console on Tshock server which is running in docker container?

So my main goal is to access Tshock console so i could run some commands on the server directly.
From what i found as soon as the command to run the server is executed there is no option to get back to the console so:
I'd like to run the server in screen mode.
Dockerfile is basicly running some bash script but i'm getting errors when i'm trying to add "screen" to it. I'm getting "bootstrap.sh: 33: bootstrap.sh: Syntax error: "fi" unexpected (expecting "then")
Entering script"
I've tried everything i could found on google but nothing works :( This is my first time when i'm doing any scripting in bash so i would be grateful for understanding :)
Here is the link to original repo: https://github.com/ryansheehan/terraria/blob/master/tshock/bootstrap.sh
I would be glad for any hints on how to make this script working or if there is any other simplier option to access the console of the server :)
I've added extra line in dockerfile to download screen so it looks like that now:
FROM alpine:3.11.6 AS base
RUN apk add --update-cache \
unzip
# add the bootstrap file
COPY bootstrap.sh /tshock/bootstrap.sh
ENV TSHOCKVERSION=v4.4.0-pre12
ENV TSHOCKZIP=TShock4.4.0_Pre12_Terraria1.4.0.5.zip
# Download and unpack TShock
ADD https://github.com/Pryaxis/TShock/releases/download/$TSHOCKVERSION/$TSHOCKZIP /
RUN unzip $TSHOCKZIP -d /tshock && \
rm $TSHOCKZIP && \
chmod +x /tshock/TerrariaServer.exe && \
# add executable perm to bootstrap
chmod +x /tshock/bootstrap.sh
FROM mono:6.8.0.96-slim
LABEL maintainer="Ryan Sheehan <rsheehan#gmail.com>"
# documenting ports
EXPOSE 7777 7878
# env used in the bootstrap
ENV CONFIGPATH=/root/.local/share/Terraria/Worlds
ENV LOGPATH=/tshock/logs
ENV WORLD_FILENAME=""
# Allow for external data
VOLUME ["/root/.local/share/Terraria/Worlds", "/tshock/logs", "/plugins"]
# install nuget to grab tshock dependencies
RUN apt-get update -y && \
apt-get install -y nuget && \
apt-get install -y screen
# rm -rf /var/lib/apt/lists/* /tmp/*
# copy game files
COPY --from=base /tshock/ /tshock/
# Set working directory to server
WORKDIR /tshock
# run the bootstrap, which will copy the TShockAPI.dll before starting the server
ENTRYPOINT [ "/bin/sh", "bootstrap.sh" ]
And here is my modified code for bootstrap.sh :
#!/bin/sh
echo "Entering script"
if [ -z "$STY" ];then
echo "Opening screen mode ..."
exec screen -dm -S terraria bin/bash "$0"
else
echo "Continuing with script in screen mode"
echo "\nBootstrap:\nworld_file_name=$WORLD_FILENAME\nconfigpath=$CONFIGPATH\nlogpath=$LOGPATH\n"
echo "Copying plugins..."
cp -Rfv /plugins/* ./ServerPlugins
WORLD_PATH="/root/.local/share/Terraria/Worlds/$WORLD_FILENAME"
if [ -z "$WORLD_FILENAME" ]; then
echo "No world file specified in environment WORLD_FILENAME."
if [ -z "$#" ]; then
echo "Running server setup..."
else
echo "Running server with command flags: $#"
fi
mono --server --gc=sgen -O=all TerrariaServer.exe -configpath "$CONFIGPATH" -logpath "$LOGPATH" "$#"
else
echo "Environment WORLD_FILENAME specified"
if [ -f "$WORLD_PATH" ]; then
echo "Loading to world $WORLD_FILENAME..."
mono --server --gc=sgen -O=all TerrariaServer.exe -configpath "$CONFIGPATH" -logpath "$LOGPATH" -world "$WORLD_PATH" "$#"
else
echo "Unable to locate $WORLD_PATH.\nPlease make sure your world file is volumed into docker: -v <path_to_world_file>:/root/.local/share/Terraria/Worlds"
exit 1
fi
fi
fi
After removing empty spaces as #KamilCuk suggested the script is running but the screen doesn't seem to be working.
Here is the output from console:
PS D:\TerrariaServer\Source\terraria\tshock> docker run --rm -p 7777:7777 -v D:/TerrariaServer/World:/root/.local/share/Terraria/Worlds --name="terraria" terraria-image -world /root/.local/share/Terraria/Worlds/TestWorld.wld
Entering screen mode
+ [ -z ]
+ echo Entering screen mode
+ screen -d -m -S terraria bin/bash bootstrap.sh
+ echo Screen mode activated
+ echo Continuing with script in screen mode
+ echo \nBootstrap:\nworld_file_name=\nconfigpath=/root/.local/share/Terraria/Worlds\nlogpath=/tshock/logs\n
+ echo Copying plugins...
+ cp -Rfv /plugins/* ./ServerPlugins
Screen mode activated
Continuing with script in screen mode
Bootstrap:
world_file_name=
configpath=/root/.local/share/Terraria/Worlds
logpath=/tshock/logs
Copying plugins...
cp: cannot stat '/plugins/*': No such file or directory
+ WORLD_PATH=/root/.local/share/Terraria/Worlds/
+ [ -z ]
+ echo No world file specified in environment WORLD_FILENAME.
+ [ -z -world /root/.local/share/Terraria/Worlds/TestWorld.wld ]
+ echo Running server with command flags: -world /root/.local/share/Terraria/Worlds/TestWorld.wld
+ mono --server --gc=sgen -O=all TerrariaServer.exe -configpath /root/.local/share/Terraria/Worlds -logpath /tshock/logs -world /root/.local/share/Terraria/Worlds/TestWorld.wld
No world file specified in environment WORLD_FILENAME.
Running server with command flags: -world /root/.local/share/Terraria/Worlds/TestWorld.wld
Error Logging Enabled.
TerrariaAPI Version: 2.1.0.0 (Protocol v1.4.0.5 (230), OTAPI 1.4.0.5)
[TShock] Info Config path has been set to /root/.local/share/Terraria/Worlds
[TShock] Info Log path has been set to /tshock/logs
TShock was improperly shut down. Please use the exit command in the future to prevent this.
TShock 4.4.0.0 (Go to sleep Patrikkk, Icy, Chris, Death, Axeel, Zaicon, hakusaro, Zack, and Yoraiz0r <3) now running.
AutoSave Enabled
Backups Enabled
Welcome to TShock for Terraria!
TShock comes with no warranty & is free software.
You can modify & distribute it under the terms of the GNU GPLv3.
[Server API] Info Plugin TShock v4.4.0.0 (by The TShock Team) initiated.
Terraria Server v1.4.0.5
Resetting game objects 1%
Resetting game objects 2%
Resetting game objects 3%
...
I'm not directly seenig major issues with your entrypoint script. But I want provide some steps I would do to hopefully resolve these kind of issues:
Avoid the line f [ -z "$#" ].
See here why.
Suggestion: f [ -z "$*" ]
Also this line is problematic too: echo "Running server with command flags: $#"
See here why.
Suggestion: echo "Running server with command flags: $*"
Sometimes it can help, to enter the container and "play around". This allows you to work in your docker container like you would do in any normal Linux shell.
Have a look at the -interactive, -i option for docker run.
Example
If you want to start your container, you can run e. g. run a bash (...or sh, or whatever shell you are using):
docker run -it <img_name> <arguments> bash
This also works with exec when your container is already running and you want to enter it.
docker exec -it <container_id> bash
Try the bash -x option when invoking your entrypoint script to get a verbose output. Sometimes it helps finding the error.
ENTRYPOINT [ "/bin/bash", "-x" "bootstrap.sh" ]

Bash file working on local computer but not working on Amazon EC2 instance

I have a simple bash file which fixes the npm permission on my system. It goes like this
npm=`npm config get prefix`
echo "$npm"
if [[ $npm = "/usr/local" ]]
then
echo "Yes it's /usr/local"
echo $npm/{lib/node_modules,bin,share}
sudo chown -R $(whoami) $npm/{lib/node_modules,bin,share}
echo "Complete"
elif [[ $npm = "/usr" ]]
then
echo "Uhhohh it's /usr gotta use a different method"
mkdir ~/.npm-global
npm config set prefix '~/.npm-global'
export PATH=~/.npm-global/bin:$PATH
source ~/.profile
echo "No"
fi
npm install -g forever
npm install -g pm2
npm install -g bower
This works fine on my machine. But not my amazon ex2 instance. I am running ubuntu 16.04 LTS on my AWS EC2 instance and Ubuntu 15.04 on my local system. The error which I get is npm-permission.sh: line 2: =/usr: No such file or directory
Which is the second line. What is the issue? Why doesn't it work?
So a change as suggested by #Cyrus in the comments I modified the script to have if [[ $npm = '' ]] which made it work. Here is the working script.
npm=$(npm config get prefix)
echo "$npm"
if [[ $npm = "/usr/local" ]]
then
echo "Yes it's /usr/local"
echo $npm/{lib/node_modules,bin,share}
sudo chown -R $(whoami) $npm/{lib/node_modules,bin,share}
echo "Complete"
elif [[ $npm = "/usr" ]]
then
echo "Uhhohh it's /usr gotta use a different method"
mkdir ~/.npm-global
npm config set prefix '~/.npm-global'
echo "export PATH=~/.npm-global/bin:$PATH" >> ~/.profile
source ~/.profile
echo "No"
fi
I still don't know how it works, but it does. Some bash expert might throw some light on it.

Sending a file as a parameter to a container in order to be compiled causes problems

I'm facing some problems when sending a file to a docker container in order to be compiled and executed.
In the folder that I'm running the command I have a "compiler" folder and a file.cpp(a basic hello world"). I'm using this command:
sudo docker run --rm -it -v /compiler/ dockcompiler file.cpp
And this is the output:
useradd: warning: the home directory already exists.
Not copying any file from skel directory into it.
chgrp: cannot access '/code': No such file or directory
chmod: cannot access '/code': No such file or directory
file does not exist
My Dockerfile:
FROM ubuntu
RUN apt-get update
RUN apt-get install -y gcc g++
ADD entrypoint.sh entrypoint.sh
ADD run-code.sh run-code.sh
ENTRYPOINT ["/bin/bash", "entrypoint.sh"]
entrypoint.sh
prog=$1
uid=$2
if [ -z $1 ]; then
echo "you must provide a file"
exit 1
fi
if [ -z $uid ]; then
uid=10000
fi
echo "127.0.0.1 $(hostname)" >> /etc/hosts
groupadd code
useradd -u "$uid" -G code -d "/compiler" -m codecube
chgrp code /code
chmod 0775 /code
cd /compiler
sudo -u codecube /bin/bash /run-code.sh $prog
And the run-code.sh file(it works if I run it in terminal):
prog=$1
if [ -z $1 ]; then
echo "you must provide a file"
exit 1
fi
if [ ! -f $1 ]; then
echo "file does not exist"
exit 1
fi
extension="${prog##*.}"
case "$extension" in
"cpp")
g++ $prog && ./a.out
;;
"c")
gcc $prog && ./a.out
;;
"go")
go run $prog
;;
"pl")
perl $prog
;;
"py")
python $prog
;;
"rb")
ruby $prog
;;
*)
echo "invalid language"
exit 1
;;
esac
Updated:
The problem is -m codecube where the it says codecube directory already exists. Also, the /code directory is not present.
If the user directory is already in place on your docker image (assuming a modified image being used here). Then just skel files copy should do the trick and you can get away without doing useradd. A post here offers more info on this.
#copy skel files
cp -r /etc/skel/. /codecube

unix bash syntax error near unexpected token 'do'

I created a unix minecraft launcher. it worked perfectly fine just an hour and a half ago (as of 9:30). then I got this:
/home/axium1998/MinecraftMegaLauncher.sh: line 14: syntax error near unexpected token ~'$'do\r''.
/home/Axium1998/MinecraftMegaLauncher.sh: line 14: 'do
I have no idea what caused this.
# If code needs to be changed, just send me a PM saying something like: Project:MinecraftMegaLauncher Line #<line number> = <changed code>
# if it works (I bet it will, but for me to learn xP )it will be replaced/fixed.
export mc=$HOME/.minecraft
export mcB=$HOME/officialBackup
export tekkit=$HOME/.technic
export tekkitB=$HOME/tekkitBackup
export ftb=$HOME/.feedthebeast
export ftbB=$HOME/ftbBackup
export options=("Official" "MagicLauncher" "Tekkit" "FTB" "Backup" "Restore" "Quit")
echo "==========MinecraftMegaLauncher=========="
echo "This currently supports the following launchers: Official, Magic, Tekkit, and FTB, and doing backups as well!"
echo "I (AXIUM1998) am not responsible for data loss/corruption while backing up/restoring. (It is still indev)"
echo "Also, if there is a launcher you want to be in this mega launcher, I will consider implementing them."
echo "BUG: Running restore twice in a row (running restore, then running it again immeditely) will erase all mc data."
cd $HOME
select optL in "${options[#]}"
do
case $optL in
"Official")
echo "Starting the Official launcher..."
java -jar minecraft.jar
;;
"MagicLauncher")
echo "Starting the MagicLauncher..."
java -jar magic.jar
;;
"Tekkit")
echo "Starting the Tekkit launcher..."
java -jar tekkit.jar
;;
"FTB")
echo "Starting the FTB launcher..."
java -jar ftb.jar
;;
"Quit")
echo "Quitting..."
break
;;
"Backup")
echo "Starting the backup..."
echo "Please input your password (Admin needed :( )"
sudo touch dv
sudo rm dv
if [ ! -d $mcB ]; then
sudo mkdir $HOME/officialBackup
fi
if [ ! -d $tekkitB ];then
sudo mkdir $HOME/tekkitBackup
fi
if [ ! -d $ftbB ]; then
sudo mkdir $HOME/ftbBackup
fi
cd $mcB
sudo rm -rf *
cd $tekkitB
sudo rm -rf *
cd $ftbB
sudo rm -rf *
sudo cp -R $mc/* $mcB/
sudo cp -R $tekkit/* $tekkitB/
sudo cp -R $ftb/* $ftbB/
echo "Backup complete"
echo "Making current user owner of files..."
sudo chown -R $USER $mcB
sudo chown -R $USER $tekkitB
sudo chown -R $USER $ftbB
echo "User $USER now can write to backed up folders"
;;
"Restore")
echo "Starting the restoration..."
echo "Admin is, again, required :( "
sudo touch dv
sudo rm dv
cd $mc
sudo rm -rf *
cd $tekkit
sudo rm -rf *
cd $ftb
sudo rm -rf *
cd $HOME
sudo mv $mcB/* $mc/
sudo mv $tekkitB/* $tekkit/
sudo mv $ftbB/* $ftb/
echo "Restore complete"
;;
*)
echo "Invalid operand.";;
esac
done
edit: may not be exact line. I changed it after I last uploaded it
My wild guess is that you converted your script to Windows format (perhaps copying it from Windows) and then you receive this error: unexpected do\r because the \r is unexpected.
Use dos2unix to convert it.

Resources