basic BASH variable call failing in script - bash

Why is this basic variable call in my script failing?
The script is just below and the errors outputted in terminal after execution are below the script.
Line 8 is the first sudo command.
I am executing this script as root in terminal for now. It works just fine if I execute the commands manually, one-at-a-time, within terminal...
I would be grateful for any insight.
#!/bin/bash
echo Enter username
read NAME
echo Enter number
read NUM
sudo (cd /Users/$NAME && tar c .) | (cd /Users/$NUM && tar xf -)
sudo chown -R $NUM:"Domain Users" /Users/$NUM
sudo chmod g+rwx /Users/$NUM
Stephen-Kucker:Desktop root# ./stackoverflowq.txt
Enter username
jsteinberg-c
Enter number
admin
./stackoverflowq.txt: line 8: syntax error near unexpected token `cd'
./stackoverflowq.txt: line 8: `sudo (cd /Users/$NAME && tar c .) | (cd /Users/$NUM && tar xf -)'

Try this:
sudo tar -C /Users/$NAME -c . | sudo tar -C /Users/$NUM -xf -

You need to use the -s option to pass an arbitrary shell command (like the pipeline shown) to the shell with sudo:
sudo -s "(cd /Users/$NAME && tar c .) | (cd /Users/$NUM && tar xf -)"

Related

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

chmod +x cant find build.sh file

I have been trying to make a VCS in C++ but the build file is not running in my LINUX(Ubuntu).
It is prompting the above message.
my build file is as follows:
#!/bin/bash
sudo apt-get update
udo apt-get install openssl -y
sudo apt-get install libssl-dev -y
mkdir -p ~/imperium/bin
cp imperium.sh ~/imperium
cd ..
make
cd ~/imperium/bin || echo "error"
chmod +x main
cd ..
if grep -q "source $PWD/imperium.sh" "$PWD/../.bashrc" ; then
echo 'already installed bash source';
else
echo "source $PWD/imperium.sh" >> ~/.bashrc;
fi
my imperium.sh file is also as follows:
function imperium(){
DIR=$PWD
export dir=$DIR
cd ~/imperium/bin || echo "Error"
./main "$#"
cd "$DIR" || echo "Error"
}
I will be heavily obliged if any one can solve this problem of mine. After chmod I have been doing:
./build.sh but its prompting that build.sh file does not exists.
For me it seems you have a typo right in the 3rd row "udo" ->
"sudo".
Also, You should avoid using cd .. and use relative paths for
the commands.

Prompt "yes" automatically in a script

I created a script for cleaning my system with pacman it works fine but I want to make response to Yes/No automatic.
Here's the script:
#!/bin/bash
# Trash cleaning script by arch
sudo pacman -Sc && sudo paccache -r &&
yay -Sc &&
rmlint ~/ && sh ~/rmlint.sh &&
sudo rm -r ~/.cache/* &&
sudo pacman -Qtdq &&
sudo pacman -R $(pacman -Qtdq)
If I type in terminal yes Y | sudo pacman -Sc it works but if I add it to script
yes Y | sudo pacman -Sc && yes Y | sudo paccache -r &&
it gives response
Cache directory: /var/cache/pacman/pkg/
:: Do you want to remove all other packages from cache? [Y/n]
Update
It was my mistake for Yay AUR packager it works fine with the following:
#!/bin/bash
# Trash cleaning script, by arch
yes Y | sudo pacman -Sc && sudo paccache -r &&
rmlint ~/ && yes | sh ~/rmlint.sh &&
sudo rm -r ~/.cache/* &&
sudo pacman -Qtdq &&
sudo pacman -R $(pacman -Qtdq)

Can't execute script from within rc.local

Within rc.local I have
sudo -H -u myUser -s -- "cd /home/myUser/parlar && /usr/local/bin/meteor &"
I want to test it but when I execute that with
myUser:~$ sudo service rc.local start
/bin/bash: cd /home/myUser/parlar && /usr/local/bin/meteor &: No such file or directory
If I execute the command
cd /home/myUser/parlar && /usr/local/bin/meteor &
it works
How can I execute rc.local so that it changes into the relevant directory, and runs the command as the requested user?
Whatever arguments you give to sudo after -- are considered as command & its arguments.
There is no command/executable named "cd /home/myUser/parlar && /usr/local/bin/meteor". You can however, start bash & run the command within that bash shell.
e.g.
sudo -H -u myUser -s -- bash -c "cd /home/myUser/parlar && /usr/local/bin/meteor &"
Since the first command is cd, this alternate approach may also work:
sudo -H -u myUser -s -i PWD=/home/myUser/parlar -- /usr/local/bin/meteor
To see the log of rc.local itself, it's better to run these commands:
**systemctl restart rc-local.service**
**systemctl status rc-local.service**
May be it can be help full for better trouble shooting

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.

Resources