How to copy bash commands from snippet without $ sign? - linux

In most of the websites, I can see snippet with a bunch of bash commands ( With a Click to Copy Button), The problem with that button is this copy $ sign too.
Just like the image about, if I click copy, it will copy complete command INCLUDING $ Sign
$ sudo apt-get update
$ sudo apt-get upgrade
$ sudo apt-get install gnome-shell
$ sudo apt-get install ubuntu-gnome-desktop
$ sudo apt-get install autocutsel
$ sudo apt-get install gnome-core
$ sudo apt-get install gnome-panel
$ sudo apt-get install gnome-themes-standard
We, programmers, are lazy in terms of doing this one by one, If I copy and paste this directly on bash it will show error since it will NOT recognize what is "$" as the command is starting with $ sign, is there a simple hack to let bash skip the first $ sign if command starts with it? If not any solution? I am tired of getting a response from the server and then pasting next commands
P.S This question is asked before I guess but that isn't EXACTLY what I have asked so moderators please understand my point.

Just cut it
while read -r line; do
$(echo "$line"|cut -d' ' -s -f2-)
done
After that, you'll just have to copy it to stdin(and end it witg Contr+D
As requested, here is a function that trims it:
function trimDollar(){
echo "$*"cut -d' ' -s -f2-
}

Related

Linux write basic commands and make it executable using single line of code

Hello is this possible to create a file with these line of codes below and make it as an executable file on single line of code? Currently I'm doing manually. Your response is highly appreciated. Thank you
Manual Steps
-vi + content
-chmod +x filename
This is the file content:
#!/bin/bash
sudo apt-get update
sudo apt install curl -y
sudo apt install -y default-jdk
Screenshot:
CLI Image
Objective to write everything using one line of code
If it is crucial that it be a one-liner and vi is not a requirement:
echo -e '#!/bin/bash\nsudo apt-get update\nsudo apt install curl -y\nsudo apt install -y default-jdk' > test.sh && chmod +x test.sh && ./test.sh
If vi is a requirement you could do something like:
vim file.txt "+i#!/bin/bash" "+osudo apt-get update" "+o..." and so on
in place of the echo, but this seems much less effective to me and I'm less familiar with using vi in this way.
You could generate a test.sh with bash script (we can call it download.sh):
# 1. write script content to test.sh
cat <<EOT >> test.sh
#!/bin/bash
sudo apt-get update
sudo apt install curl -y
sudo apt install -y default-jdk
EOT
# 2. make it executable
chmod +x test.sh
When you execute bash download.sh, it will generate a test.sh automatically.

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.

how to provide an input for whiptail using commhe and line

So, I have to setup several server that requires mysql-server on each and every one of them. I decided to write a shell script to enter a input to whiptail which require the user to input password. I wrote the bash script but it just does not response when it reach the whiptail page, can anyone help me with this? Thanks
#!/bin/bash
sudo apt-get install mysql-server <<< "yes
mypassword
"
First when the sudo apt-get ask about whether to install or not it supplies yes, but then when it came to whiptail it does not provide any input as it should be which is mypassword
Rather than providing input to whiptail, you can run apt-get install like this to skip prompts during installation:
DEBIAN_FRONTEND="noninteractive" apt-get install -y mysql-server
If you want to provide a root password as well, you can do this:
export DEBIAN_FRONTEND="noninteractive"
sudo debconf-set-selections <<< "mysql-server mysql-server/root_password password myrootpw"
sudo debconf-set-selections <<< "mysql-server mysql-server/root_password_again password myrootpw"
sudo apt-get install -y mysql-server
Take care to not accidentally expose your root password (i.e. ensure it doesn't end up in your history, securely delete any script files containing it, I'm sure there are others).
I found this information here.

Multiple commands in a single line is not working when Sudo command is used

I tried to execute the multiple commands in a single line using &&, but it isn't working in the following case. It just executes the first sudo command alone.
sudo /usr/bin/rootsh -i -u ditest && Set_proj && 1
Note: Only the first one is Sudo command and the remaining are a normal ones. I already tried using the following command. But it doesn't works
sudo /usr/bin/rootsh -i -u 'ditest && Set_proj && 1'
Multiple command doesn't works even though if I enter "Set_Proj && 1" after executing the first command. Only the first command is executed (Set_proj).
In other cases multiline commands are working fine. Eg: cd jtest && ls
Edited:
Its not a duplicate of how to run two commands in sudo?. I described clearly that I haven't used multiple sudo commands. Once logged into putty, the command sequence will be
jacob:/home/users/jacob: sudo /usr/bin/rootsh -i -u ditest
[ditest] ditest> Set_Proj ***List of projectes displayed***
> 1 ***Select any project***
simple. Put one sudo by command:
sudo apt-get update && sudo apt-get dist-upgrade -y
Usually, only the first sudo ask for password, andit cached for next sudos
Use bash -c, like this
sudo bash -c "ditest && Set_proj && 1"
If you want to run sudo as another user then,
sudo -H -u user bash -c "ditest && Set_proj && 1"

Resources