Prompt "yes" automatically in a script - bash

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)

Related

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.

Need assistance in converting Makefile to Shell script

Need your help to convert code in Makefile to Shell script? Please help as I am new to both MakeFile and Shell Scripting.Thanks.
Sample Code:
include Configfile
.PHONY: config-arch asoc-tool clone-repo generate-irx api-login \
upload-file get-app run-scan show-scan-id get-asset-group create-app
config-arch:
sudo dpkg --add-architecture i386
sudo apt-get update
sudo apt-get install libc6:i386 libncurses5:i386 libstdc++6:i386
asoc-tool: config-arch
$(eval DIR := $(shell pwd))
curl -o client.zip $(APPSCAN_TOOL)
mkdir client ; mkdir tool
unzip -qq client.zip -d client
cd client ; ls | xargs -I {} sh -c "cp -r {}/* $(DIR)/tool"
rm -rf client
clone-repo:
git clone $(GIT_REPO)
# Generates the irx file for icp-cert-manager.
generate-irx:
$(eval DIR := $(shell pwd))
cd $(PROJECT_NAME); $(DIR)/tool/bin/appscan.sh prepare $(flag)
#!/bin/sh
# config-arch
sudo dpkg --add-architecture i386
sudo apt-get update
sudo apt-get install libc6:i386 libncurses5:i386 libstdc++6:i386
# asoc-tool
curl -o client.zip $(APPSCAN_TOOL)
mkdir client ; mkdir tool
unzip -qq client.zip -d client
cd client ; ls | xargs -I {} sh -c "cp -r {}/* ./tool"
rm -rf client
# clone repo
git clone ${GIT_REPO} # you should overwrite ${GIT_REPO} to your git repo, maybe from Configfile
# generate-irx
cd $(PROJECT_NAME)
./tool/bin/appscan.sh prepare ${flag} # you should check your ${flag}, maybe from Configfile

bash script unexpected end of file pterodactyl

I am trying to run a custom egg through the Pterodactyl panel however, I get the error "/entrypoint.sh: line 30: syntax error: unexpected end of file"
My Docker image is as followed;
FROM ubuntu:18.04
MAINTAINER Amelia, <me#amelia.fun>
RUN apt-get update -y
RUN DEBIAN_FRONTEND=noninteractive apt-get install -y dos2unix curl gnupg2 git-core zlib1g-dev libssl-dev libreadline-dev libyaml-dev libsqlite3-dev sqlite3 libxml2-dev libxslt1-dev libcurl4-openssl-dev libffi-dev yarn build-essential gpg-agent zip unzip software-properties-common git default-jre python3-pip python-minimal python-pip ffmpeg libopus-dev libsodium-dev libpython2.7 libpython2.7-dev wget php7.2 php7.2-common php7.2-cli php7.2-fpm
RUN curl -sL https://deb.nodesource.com/setup_10.x -o nodesource_setup.sh
RUN bash nodesource_setup.sh
RUN apt-get install -y nodejs
RUN rm -rf nodesource_setup.sh
RUN adduser -D -h /home/container container
USER container
ENV USER=container HOME=/home/container
WORKDIR /home/container
COPY ./entrypoint.sh /entrypoint.sh
CMD ["/bin/bash", "/entrypoint.sh"]
and my entrypoint.sh is as followed;
#!/bin/bash
cd /home/container
MODIFIED_STARTUP=`eval echo $(echo ${STARTUP_PARAMETERS} | sed -e 's/{{/${/g' -e 's/}}/}/g')`
rm -rf *
git clone ${REPO_PARAMETERS}
cd */
if grep -q 'Java' AppType
then
${STARTUP_PARAMETERS}
if grep -q 'PHP' AppType
then
${STARTUP_PARAMETERS}
elif grep -q 'Python2' AppType
then
[ -f "requirements.txt" ] && pip2 install -r requirements.txt ${STARTUP_PARAMETERS} || ${STARTUP_PARAMETERS}
elif grep -q 'Python3' AppType
then
[ -f "requirements.txt" ] && pip3 install -r requirements.txt ${STARTUP_PARAMETERS} || ${STARTUP_PARAMETERS}
elif grep -q 'NodeJS' AppType
then
npm install
${STARTUP_PARAMETERS}
else
echo "Application not supported"
fi
echo "${MODIFIED_STARTUP}"
the Bash file is nowhere near 30 lines long so I'm not really sure.
The guide I used can also be found here
The immediate problem is that you have two if statements, but only one of them is closed with fi; it looks to me like the second one should be elif. But there are a number of other things that look like bad ideas to me:
cd commands in scripts should (almost) always have error tests -- for example, if cd /home/container fails for some reason, the rest of the script (including rm -rf *) will run in an unexpected location. Now, a self-destroying Docker environment may not be as big a deal as a self-destroying real system, but it's still not a good thing. I'd use something like this instead:
cd /home/container || {
echo "Error -- can't move to /home/container, something rotten in Denmark." >&2
exit 1
}
A similar comment applies to cd */.
The next line, that sets MODIFIED_STARTUP, is a mishmash of bad ideas. I'm not familiar with what's going to be in $STARTUP_PARAMETERS, but in general: Use $( ) instead of backticks (and not a weird mix of both). echo $(somecommand) is pretty much a no-op, just run the command directly. Also, variable references (and similar expansions like $( )) should almost always be in double-quotes (exception: on the right side of an assignment). And eval is generally dangerous, and should be avoided if possible. I you give me an example of what $STARTUP_PARAMETERS looks like, I could probably give a cleaned-up version of this.
The big if ... elif... etc has several conditions that do the same thing, e.g.
elif grep -q 'Python2' AppType
then
[ -f "requirements.txt" ] && pip2 install -r requirements.txt ${STARTUP_PARAMETERS} || ${STARTUP_PARAMETERS}
elif grep -q 'Python3' AppType
then
[ -f "requirements.txt" ] && pip3 install -r requirements.txt ${STARTUP_PARAMETERS} || ${STARTUP_PARAMETERS}
On the DRY principle (Don't Repeat Yourself), it'd be better to have a single test for all equivalent situations, like this:
elif grep -q 'Python2' AppType || grep -q 'Python3' AppType
then
[ -f "requirements.txt" ] && pip2 install -r requirements.txt ${STARTUP_PARAMETERS} || ${STARTUP_PARAMETERS}
or even:
elif grep -q 'Python[23]' AppType
then
[ -f "requirements.txt" ] && pip2 install -r requirements.txt ${STARTUP_PARAMETERS} || ${STARTUP_PARAMETERS}
BTW, the use of ${STARTUP_PARAMETERS} without quotes is setting off warning bells for me here, but may be inevitable -- again, I don't know its format. And the && ... || construction isn't always a safe replacement for if then else fi, since it can run both branches. In this script, if requirements.txt exists but the pip2 install command fails, it'll go ahead and run ${STARTUP_PARAMETERS} as well. Is that intentional? If not, I'd use a proper if statement instead.

Run multiple commands, but run the last one in the background

I have 4 commands I want to run:
sudo mkdir -p /data/db && \
sudo chmod 755 /data/db && \
sudo chown -R addison.pan: /data && \
mongod &
I only want to run mongod in the background if the other 3 above it succeed. But when I type this into bash, it runs the whole thing as one background task. How do I only make the mongod run in the background, and only if it gets to it?
To run one or more commands in a separate process, enclose that series of commands in parentheses. As specified in the Single Unix Specification, §2.9.4 “Compound Commands”:
( compound-list )
Execute compound-list in a subshell environment […]
To group one or more commands in the same shell process, enclose that series of commands in curly braces:
{ compound-list ; }
Execute compound-list in the current process environment. […]
That's true for any POSIX shell (so it also works in Bash).
So your example can be changed to:
sudo mkdir -p /data/db && \
sudo chmod 755 /data/db && \
sudo chown -R addison.pan: /data && \
( mongod & )
That may be good because you want the mongod process separated. On the other hand, a more general answer would be to group the list of commands within the same shell process:
sudo mkdir -p /data/db && \
sudo chmod 755 /data/db && \
sudo chown -R addison.pan: /data && \
{ mongod & }
Both these are described in the above documentation references.
Be explicit. There's no need to try to abuse the syntax to use a short-circuit:
if \
sudo mkdir -p /data/db \
&& sudo chmod 755 /data/db \
&& sudo chown -R addison.pan: /data
then
mongod &
fi
Use parens:
sudo mkdir -p /data/db && \
sudo chmod 755 /data/db && \
sudo chown -R addison.pan: /data && \
(mongod &)
You could run multiple commands within sudo, for example:
sudo sh -c 'mkdir -p /data/db && chmod 755 /data/db && chown -R <user>: /data' \
&& mongod &

basic BASH variable call failing in script

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 -)"

Resources