Running bash script in another directory - linux

I know this question has been asked many times before but I have not been able to get my code working.
I am using the Raspberry Pi 3, with a CAN-BUS Shield. As this will be going into a production environment I need the Pi setup to be nice and easy. I have started to write a bash script so the production staff can run the script and the Pi will update and install everything it needs from the one script.
I have been following this web site https://harrisonsand.com/can-on-the-raspberry-pi/ and I have run into a problem when it comes to compiling can-utils.
I am able to clone the can-utils.git from here https://github.com/linux-can/can-utils.git
by using sudo git clone https://github.com/linux-can/can-utils.git
but my issues come when I need to run the ./autogen.sh & the ./configure as these are located in the dir can-utils.
If I run this from the Pi terminal as described on the web site, it works fine as I change dir cd can-utils and then just sudo ./autogen.sh but it isn't working when I run it in the bash script.
Below is the script I have so far, I know that most of it is commented out this is so that I can test each part as I write it and don't need to constantly download and install stuff I already have
#!/bin/bash
## Change Password
#printf "***********************************************************************\n"
#printf "Changing Password\n"
#echo "pi:***********" | sudo chpasswd # Password hidden
#sleep 1
#printf "Password Changed\n"
## Update & Upgrade Pi
#printf "***********************************************************************\n"
#printf "Update & Upgrade Pi\n\n"
#sudo apt-get update && sudo apt-get upgrade -y
#sleep 1
## Upgrade dist
#printf "***********************************************************************\n"
#printf "Upgrade Dist\n\n"
#sudo apt-get dist-upgrade -y
#sleep 1
## Install libtools
#printf "***********************************************************************\n"
#printf "Installing libtools\n\n"
#sudo apt-get install git autoconf libtool -y
#sleep 1
## Download required files
#printf "***********************************************************************\n"
#printf "Downloading required files\n\n"
## can-utils
#sudo git clone https://github.com/linux-can/can-utils.git
#sleep 1
## Auto configure can-utils
printf "***********************************************************************\n"
printf "Auto Configure can-utils\n\n"
# Things I have tried and do not work
#(cd /c && exec /can-utils/autogen.sh)
#sudo source /can-utils/autogen.sh
#sudo ./can-utils.autogen.sh
sleep 1
When I try the sudo ./can-utils.autogen.shin the Pi terminal the script starts to work so I think this is sort of the right command I need but then I get an error autoreconf: 'configure.ac or 'configure.in' is required these files are in the can-utils dir but for some reason it can't find them. Please can someone help me I have been searching for the answer for the last 2 days

Thank you for your help, rightly or wrongly I have ended up using cd /home/pi/can-utils I had thought I had tried that in the past but I think cd ./can-utils which didn't work.

sudo with script is for me, a nightmare. I just read in the man of sudo of my fedora 25:
Running shell scripts via sudo can expose the same kernel bugs that make setuid shell scripts unsafe on some operating systems (if your OS has a /dev/fd/ directory, setuid shell scripts are generally safe).
sudo command should protect the root account to avoid to run scripts written by user to gain root privilege.
If you keep the use of sudo, my advise should to add a cd command on top of your script:
cd /where_everithing_is
to be sure to be in the right place.
But, may be, sudo will fight again against you !

Related

Change directory in a startup script debian

I am running a debian VM Instance using GCP Compute Engine and I have added a automation script to be executed on startup.
There are few tools which will be downloaded on startup. Only issue is, everything is getting downloaded in / directory.
I need to download everything in $HOME directory.
Different ways I have tried
#!/bin/bash
set -x
cd $HOME
mkdir $HOME/test
cd $HOME/test
apt install wget -y
wget https://download.java.net/openjdk/jdk11/ri/openjdk-11+28_linux-x64_bin.tar.gz
#!/bin/bash
set -x
source $HOME
mkdir $HOME/something
#!/bin/bash
set -x
cd $HOME
mkdir $HOME/something
exec bash
Still it is downloaded in / directory. What else can be done here?
You are trying to make 2 things : install wget package and download another one.
Why don't you tried to install wget manually ?
apt-get install wget
You have then to store the full path for your script, and download the package needed it it. Try this :
#!/bin/bash
homePath=$HOME
mkdir $HOME/test
wget https://download.java.net/openjdk/jdk11/ri/openjdk-11+28_linux-x64_bin.tar.gz -P $homePath/test/

Not working „source ~/.profile“ inside bash script

To permanently update ~/.profile with source, only working on manual input. Also reboot of the whole system won‘t update ~/.profile and i need to update it manual.
Is there a special code style to use it as working code inside a bash/shell script or is this special code not intended to be used in automated scripts?
Need it to automate installation of golang.
In the following code the line "source ~/.profile" won't work, and without any error messages, the rest is working fine:
#!/bin/bash
sudo apt update
sudo apt -y upgrade
cd ~
curl -O https://dl.google.com/go/go1.12.5.linux-amd64.tar.gz
tar xvf go1.12.5.linux-amd64.tar.gz
sudo chown -R root:root ./go
sudo mv go /usr/local
cd ~
sudo rm go1.12.5.linux-amd64.tar.gz
sudo echo "export GOPATH=\$HOME/work" >> ~/.profile
sudo echo "export PATH=\$PATH:/usr/local/go/bin:\$GOPATH/bin" >> ~/.profile
source ~/.profile
Preferred:
Source the script itself rather than running it - then the commands in the script are run in the current shell, including the source ~/.profile.
Alternative (since this replaces the running shell, history, variable values, and other state will be lost. So there should be a very good reason to use this method):
Use exec bash or something similar instead of source ~/.profile - this replaces the currently running Bash with another instance which will itself load the new .profile.
Here is a refactoring which defers the decision to the user and cleans up the script somewhat.
#!/bin/bash
# Put this in a variable so there is only one place to update
tarball='go1.12.5.linux-amd64.tar.gz'
sudo apt update
sudo apt -y upgrade
# cd || why would you?
curl -O "https://dl.google.com/go/$tarball"
tar xvf "$tarball"
sudo chown -R root:root ./go
sudo mv go /usr/local
rm "$tarball"
printf '%s\n' "export GOPATH=\$HOME/work" \
"export PATH=\$PATH:/usr/local/go/bin:\$GOPATH/bin" >> ~/.profile
echo "$0: done. source ~/.profile or exec bash to activate new settings." >&2

Ubuntu Script run a series of script one line by one line automatically

I am a new Ubuntu user.
Recently, I try to set up a server on Ubuntu.
I am wondering how to write a automatically script to run a series of script one by one.
For example, I need to install squid first, after that I need to make a copy of config file then modify the file. The following are the steps that I write in the command console. I wonder how to make a script to run that automatically.
sudo apt-get install squid -y;
cd /etc/squid3;
sudo cp squid.conf squid.conf.bak;
sudo rm -rf squid.conf;
sudo nano squid.conf
Just add a shebang, place everything in a ".sh" file, make the file executable, and run it...
Save this as test.sh
#!/bin/bash
sudo apt-get install squid -y;
cd /etc/squid3;
sudo cp squid.conf squid.conf.bak;
sudo rm -rf squid.conf;
sudo nano squid.conf
Make it executable chmod +x test.sh
Run it... ./test.sh
To edit the file from a terminal
Get a terminal on the box where you want the script to live. Probably you will SSH into it.
Then just cd to the path you want the script to live and do the following...
nano test.sh This opens the nano terminal text editor.
Copy the above test.sh commands, make sure to get the shebang (#!/bin/bash).
Paste the script into the nano editor, you'll need to use ctrl+v or cmd+v.
Hit the key combination of ctrl + o, hit the enter key.
Hit the key combination of ctrl + w. This exits nano. Proceed with the abov instructions.
I suggest you read up on nano so you can get more familiar with its abilities as it can save a lot of time!
I have wrote some script for my VPS and this is a example for Squid3
#!/bin/bash
function add_user () {
while true; do
echo -e "\nInsert a name for the Squid3 user (0=exit): \c"
read utente
case "$utente" in
0)
echo -e "\nGoodbye $USER!\n"
exit 0
;;
*\ *)
echo -e "\nYou can't use spaces in the name!"
sleep 2
continue
;;
*)
break
;;
esac
done
if [ ! -e '/etc/squid3/.passwd' ]; then
sudo htpasswd -c /etc/squid3/.passwd $utente
else
sudo htpasswd /etc/squid3/.passwd $utente
fi
}
function installer () {
sudo apt-get install squid3 apache2-utils -y
sudo bash -c "echo 'here
you
must
paste
your
configuration
file' > /etc/squid3/squid.conf"
sudo service squid3 restart
}
if ! [ "$(sudo which squid3)" ]; then
installer
add_user
else
add_user
fi
First run it install squid3 and apache2-utils (for htpasswd) and after make a new user.
If you run it again you can add more users.

Syntax error when running .sh script on linux. What am I doing wrong?

I've been attempting to write a shell script to detect composer and git on a virtual linux = Ubuntu 16.0.4 machine and then install them if needed. + clone the required repository if the machine is ready for it.
Now this is my first attempt to write any kind of script and also sorry if somehow I messed up the question itself, I'm quite now on stackoverflow as well.
Any help is appreciated, thanks in advance.
Here's the original task specification I received initially:
-check if git is installed on server
if so, clone repo with codebase
-check if composer is installed on server
if so, composer install in the root directory of the laravel application
-finally, php artisan migrate --seed
Now this is how I was trying to achieve this:
#!/bin/bash
echo "The installation process is about the begin..."
if ! which git;
then echo "Git has been located on the destination system. Cloning begins..."
git clone <link to the gitlabe repo>
else echo "There is no Git installed on the system. Git installation commences..."
sudo apt-get update
sudo apt-get upgrade
sudo apt-get install git
echo "Cloning begins..."
git clone <link to the gitlabe repo>
fi
if ! which composer;
then
echo "Composer has been located on the destination system."
else
echo "There is no Composer installed on the system. Composer installation commences..."
sudo apt-get update
sudo apt-get upgrade
sudo apt-get install composer
fi
sudo apt-get update
sudo apt-get install curl php5-cli git
curl -sS https://getcomposer.org/installer | sudo php -- --install- dir=/usr/local/bin --filename=composer
composer global require "laravel/installer"
sudo apt-get update
'Now the preferred version in the vagrantfile has to be edited to be 1.8.1 instead of 1.9'
'Generate a ssh key'
ssh-keygen -t rsa -b 4096 -C "< e-mail adress that I used >"
'Start ssh agent eval $'
ssh-agent -s
'Add your SSH private key to the ssh-agent'
ssh-add -K ~/.ssh/id_rsa
php artisan migrate --seed
The error message I recieve:
sudo sh ./testscript.sh
[sudo] password for linuxtest:
The installation process is about the begin...
: not foundt.sh: 3: ./testscript.sh:
: not foundt.sh: 4: ./testscript.sh:
: not foundt.sh: 5: ./testscript.sh:
./testscript.sh: 72: ./testscript.sh: Syntax error: end of file unexpected (expecting "then")
The answer that helped me solve my problem was posted by Charles Duffy in a comment:
This looks like your file has DOS rather than UNIX newlines. This will prevent syntax like fi from being recognized, because it's read as fi$'\r', which isn't a keyword.
#!/bin/bash
echo "The installation process is about the begin...";
if [ -x "$(command -v git)" ]; then
echo "Git has been located on the destination system. Cloning; begins..."
git clone <link to the gitlabe repo>;
else
echo "There is no Git installed on the system. Git installation commences...";
sudo apt-get update && sudo apt-get upgrade && sudo apt-get install git;
echo "Cloning begins...";
git clone <link to the gitlabe repo>;
fi
if [ -x "$(command -v composer)" ]; then
echo "Composer has been located on the destination system.";
else
echo "There is no Composer installed on the system. Composer installation commences...";
sudo apt-get update && sudo apt-get upgrade && sudo apt-get install composer;
fi
Hey there
I think this should fix your If condition problem. If you want to know more about how you should check for the case that a programm exists, look here:
Check if a program exists from a Bash script
Its the second answer for a quickfix.
Always remember to chain commands which are depending on the success of the preceding command via "&&". This secures that the next command will just be executed if the preceding doesn't fail.
I recommend doing it the same way with the ssh commands.
#edit
also make sure that you end each command with a semicolon.
hope I could help.

bash execute code stopped after execute ' sudo -i'

# !/bin/bash
sudo -i
cd /etc/apt/sources.list.d
echo "deb http://archive.ubuntu.com/ubuntu/ precise main restricted universe multiverse" >ia32-libs-precise.list
sudo apt-get update
sudo apt-get install ia32-libs
sudo rm /etc/apt/sources.list.d/ia32-libs-raring.list
sudo apt-get update
when I execute this script ,it just do 'sudo -i ' then stop, who can help me ?
The sudo manpage says :
-i,--login
Run the shell specified by the target user's password data‐base entry as a login shell.
.
.
.
If no command is specified, an interactive shell is executed.
No wonder the execution of your script stops.
The commands
cd /etc/apt/sources.list.d
.
.
sudo apt-get update
are never reached because you have just spawned a new shell with sudo -i.
As [ #mona_sax ] suggested in comment,running a script as sudo may not be a good idea in the security context. It's not clear what your actual intention is, but if the intention is to run the script in background then remove sudo -i line and do :
./script 2>&1 1>/dev/null &
Because you don't specify a command to run as root, sudo invokes an interactive shell. It won't terminate until you exit from it (or it is killed by a signal, etc).
If you need it to return immediately, you could pass true as the command:
sudo true
However, in your case, it's probably better, given what you're doing, to just limit the script to only superusers:
#!/bin/sh
set -e
# check we are running as root
if [ $(id -u) != "0" ]; then
echo "ERROR: this script must be run as a superuser" >&2
exit 1
fi
Then it is up to the user to gain appropriate privileges, rather than encoding that into the script.

Resources