bash script through docker cant cd into existing directory - bash

I have a docker file
which end with the following
COPY . /research
COPY ./funnel_viewer/run_dashboard.sh /
RUN chmod +x /run_dashboard.sh
ENTRYPOINT ["sh", "/run_dashboard.sh"]
the run_dashboard.sh script looks like:
#!/bin/bash
echo $(pwd)
echo $(ls -l)
cd ./funnel_viewer
/opt/conda/envs/dashboard_env/bin/python dashboard.py run
When running the docker I get 'cant cd to funnel_viewer'
This is while the echo ${ls -1} shows directory exists.
Directories owner is root and so is user running the script.
Why am I getting the cand cd to.. error?

Related

Creating multiple directories with shell script results in odd directory structure

I wrote a simple bash script to create some directories, like so:
#!/bin/sh
PROJECT_DIR=$(cd "$(dirname "$0")" && pwd)
cd ${PROJECT_DIR} || exit 1
mkdir -p Website/{static/{cs,js},templates/{html,xhtml}}
However, after I run the script (./script.sh), the directory structure looks like this:
There is no syntax error in my script. When I try the mkdir command directly on the terminal, the directories are created correctly.
Why is the bash script run behaving this way?

Server shell backup script (bash)

Name of a script - backup_script.sh
Location of a script on server - /home/company_folder/company_site_backups
Line added to the cron file:
#monthly /home/company_folder/company_site_backups/backup_script.sh
#!/bin/bash
DIR="/home/company_folder/company_applications/*"
BACKUPDIR="/home/company_folder/company_site_backups"
NOW=`date +\%Y\%m\%d`
cd $DIR
for i in $DIR; do zip -r "${i%/}.zip" "$BACKUPDIR/$i-$NOW"; done
ls -l
echo "Done!"
But unfortunately my script does not work properly. Actually. It does not run at all! I do not see any errors in the syntax.
Does anyone know how to fix it?
The cd $DIR seems strange; if the first entry found by /home/company_folder/company_applications/* is a directory it will change to that directory; if it is a file (or company_applications is empty) it will get an error.
Perhaps everything is running correctly except that because of the above your ls -l is not running in the directory you expect? Try removing the cd and changing it to ls -l $DIR.
It also seems very strange to me that you are zipping up content from a backup directory into an applications directory. Perhaps you meant to be doing:
zip -r "$BACKUPDIR/`basename $i`-$NOW" $i
could you try this;
#!/bin/bash
DIR="/home/company_folder/company_applications/*"
BACKUPDIR="/home/company_folder/company_site_backups"
NOW=`date +\%Y\%m\%d`
cd $DIR
for i in $DIR
do
base=$(basename "$i")
zip -r $BACKUPDIR/$base-${NOW}.zip $i
done
ls -l $BACKUPDIR
echo "Done!"

Convert bashrc function into a script/command?

I created the following function in a bash terminal as a way to move and immediately see the files in a directory.
function cnl { (cd $* ; pwd ; ls --color) }
It works fine as an addition to .bashrc, but I would like to turn it into a command that can be called from a script in my ~/bin directory.
Create a file in ~/bin with this content:
#!/bin/bash
cd $*
pwd
ls --color
and make it executable:
chmod u+x ~/bin/your_script

create a shell script with a shell script (mac)

I've been creating mac shell executables with this method:
Create a file;
Add #!/bin/sh;
Add the script;
Run chmod 755 nameofscript.
I now need to create a shell script to create a shell script in another directory and make it executable (with chmod) so that it can be run on startup.
#!/bin/sh
dir=/tmp
fnam=someshellscript
echo '#!/bin/sh' > $dir/$fnam
echo 'find /bin -name "*X*"' >> $dir/$fnam
chmod 755 $dir/$fnam
#!/bin/sh
echo "script goes here!" > /path/to/place
chmod 755 /path/to/place
?

Bash script to change parent shell directory [duplicate]

This question already has answers here:
Why can't I change directories using "cd" in a script?
(33 answers)
Closed 7 years ago.
What I'm trying to do
I've created a shell script that I've added to my $PATH that will download and get everything setup for a new Laravel project. I would like the script to end by changing my terminal directory into the new project folder.
From what I understand right now currently it's only changing the directory of the sub shell where the script is actually running. I can't seem to figure out how to do this. Any help is appreciated. Thank you!
#! /usr/bin/env bash
echo -e '\033[1;30m=========================================='
## check for a directory
if test -z "$1"; then
echo -e ' \033[0;31m✖ Please provide a directory name'
exit
fi
## check if directory already exist
if [ ! -d $1 ]; then
mkdir $1
else
echo -e ' \033[0;31m✖ The '"$1"' directory already exists'
exit
fi
# move to directory
cd $1
## Download Laravel
echo -e ' \033[0;32m+ \033[0mDownloading Laravel...'
curl -s -L https://github.com/laravel/laravel/zipball/master > laravel.zip
## Unzip, move, and clean up Laravel
echo -e ' \033[0;32m+ \033[0mUnzipping and cleaning up files...'
unzip -q laravel.zip
rm laravel.zip
cd *-laravel-*
mv * ..
cd ..
rm -R *-laravel-*
## Make the /storage directory writable
echo -e ' \033[0;32m+ \033[0mMaking /storage directory writable...'
chmod -R o+w storage
## Download and install the Generators
echo -e ' \033[0;32m+ \033[0mInstalling Generators...'
curl -s -L https://raw.github.com/JeffreyWay/Laravel-Generator/master/generate.php > application/tasks/generate.php
## Update the application key
echo -e ' \033[0;32m+ \033[0mUpdating Application Key...'
MD5=`date +”%N” | md5`
sed -ie 's/YourSecretKeyGoesHere!/'"$MD5"'/' application/config/application.php
rm application/config/application.phpe
## Create .gitignore and initial git if -git is passed
if [ "$2" == "-git" ]; then
echo -e ' \033[0;32m+ \033[0mInitiating git...'
touch .gitignore
curl -s -L https://raw.github.com/gist/4223565/be9f8e85f74a92c95e615ad1649c8d773e908036/.gitignore > .gitignore
# Create a local git repo
git init --quiet
git add * .gitignore
git commit -m 'Initial commit.' --quiet
fi
echo -e '\033[1;30m=========================================='
echo -e ' \033[0;32m✔ Laravel Setup Complete\033[0m'
## Change parent shell directory to new directory
## Currently it's only changing in the sub shell
filepath=`pwd`
cd "$filepath"
You can technically source your script to run it in your parent shell instead of spawning a subshell to run it. This way whatever changes you make to your current shell (including changing directories) persist.
source /path/to/my/script/script
or
. /path/to/my/script/script
But sourcing has its own dangers, use carefully.
(Peripherally related: how to use scripts to change directories)
Use a shell function to front-end your script
setup () {
# first, call your big script.
# (It could be open-coded here but that might be a bit ugly.)
# then finally...
cd someplace
}
Put the shell function in a shell startup file.
Child processes (including shells) cannot change current directory of parent process. Typical solution is using eval in the parent shell. In shell script echo commands you want to run by parent shell:
echo "cd $filepath"
In parent shell, you can kick the shell script with eval:
eval `sh foo.sh`
Note that all standard output will be executed as shell commands. Messages should output to standard error:
echo "Some messages" >&2
command ... >&2
This can't be done. Use exec to open a new shell in the appropriate directory, replacing the script interpreter.
exec bash
I suppose one possibility would be to make sure that the only output of your script is the path name you want to end up in, and then do:
cd `/path/to/my/script`
There's no way your script can directly affect the environment (including it's current directory) of its parent shell, but this would request that the parent shell itself change directories based on the output of the script...

Resources