why bash changing my command 'mysql*' to 'mysql.sql'? - linux

I am sure that there is no such file called mysql.sql in the directory
The operating system infor
cat /etc/redhat-release
CentOS Linux release 7.3.1611 (Core)
I have a shell script. One line is returning nong zero code
yum install -y mysql* 1>/dev/null 2>/dev/null
When I run this line in terminal, everything is fine and the return code( echo $?) is zero.
But in script, it will return 1 and whith the -x option, I can see bash changing the line into
yum install -y mysql.sql
So why mysql* is changed to mysql.sql?

There is a file called mysql.sql in the current directory of the script.
Quote the argument to avoid bash expanding it, so that yum can.
yum install -y "mysql*" 1>/dev/null 2>/dev/null
In general, you always want to quote arguments with * if you don't want them expanded as a glob. Bash by default passes them through literally if they don't match anything, but you can't usually guarantee that, so it's best to just quote it.

Related

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

how to install anaconda / miniconda on Linux silently

How do I install the anaconda / miniconda without prompts on Linux command line?
Is there a way to pass -y kind of option to agree to the T&Cs, suggested installation location etc. by default?
can be achieved by bash miniconda.sh -b (thanks #darthbith)
The command line usage for this can only be seen with -h flag but not --help, so I missed it.
To install the anaconda to another place, use the -p option:
bash anaconda.sh -b -p /some/path
AFAIK pyenv let you install anaconda/miniconda
(after successful instalation)
pyenv install --list
pyenv install miniconda3-4.3.30
For a quick installation of miniconda silently I use a wrapper
script script that can be executed from the terminal without
even downloading the script. It takes the installation destination path
as an argument (in this case ~/miniconda) and does some validation too.
curl -s https://gist.githubusercontent.com/mherkazandjian/cce01cf3e15c0b41c1c4321245a99096/raw/03c86dae9a212446cf5b095643854f029b39c921/miniconda_installer.sh | bash -s -- ~/miniconda
Silent installation can be done like this, but it doesn't update the PATH variable so you can't run it after the installation with a short command like conda:
cd /tmp/
curl -LO https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh
bash Miniconda3-latest-Linux-x86_64.sh -b -u
Here -b means batch/silent mode, and -u means update the existing installation of Miniconda at that path, rather than failing.
You need to run additional commands to initialize PATH and other shell init scripts, e.g. for Bash:
source ~/miniconda3/bin/activate
conda init bash

What does this command mean?

While installing QT, I met this command:
sudo -s chmod u+x QtSdk-offline-linux-x86_64-v1.2.1.run
I wonder what -s means here.And it seems similar here:
sudo -s ./QtSdk-offline-linux-x86_64-v1.2.1.run -style cleanlooks
sudo -s runs a shell with root privileges
"The -s (shell) option runs the shell specified by the SHELL
environment variable if it is set or the shell as specified in
passwd(5). If a command is specified, it is passed to the shell for
execution. Otherwise, an interactive shell is executed."
From here.
As mentioned in the comments above, check out the man info:
Type man sudo to your command line.
Find -s in the list of commands for a good explanation.

Linux - Password as script arguments and autoinput

I'm trying to execute a Linux shell script through my java program and i'd like my script to take as argument the root's password. As my Linux knowledge goes i know that if i make the command sudo apt-get update -y he updates everything without prompting me for confirmation.
Is there any way to make the same but with the password? Whenever the script prompts for password he automatically reads the argument and inputs it?
EDIT: If i create a script to execute my jar file with sudo (ex: sudo javac -jar /mnt/raid/program.jar), do my scripts called upon get sudo rights aswell? As of an inheritance.
Sudo has the -S argument. From the sudo man page:
The -S (stdin) option causes sudo to read the password from
the standard input instead of the terminal device. The password
must be followed by a new-line character.
You could do something like:
echo PASSWORD | sudo -S COMMAND

What to do if dpkg nor apt-get will remove the program on Ubuntu?

The problem is that I installed a .deb file, and when I tried getting rid of it with dpkg -r ..., dpkg claimed to have removed it. Nevertheless, I can type in the "removed" command, and it still works.
I need to get it off, because I realized what I needed was a larger program that included it. When I try to run make on the larger program, it attempts to use the smaller with different options (the larger appears to be assuming a later version of the smaller).
Anyway, it's just weird that I can't get rid of it. I've re-installed and tried using the purge option, tried apt-get clean, tried restarting the machine, etc.
Any ideas would be appreciated. Thanks!
Try this:
rm /var/lib/dpkg/info/program.*
dpkg --remove --force-remove-reinstreq program
Replace 'program' with the one you want to remove.
Thanks H2CO3: "If everything else fails, perhaps delete the executable manually.... executable files [are] in the search paths of the shell which aren't executed if nonexistent"
rm `which flop`
flop is the name of the program.
WARNING!!!: Do this only if you know that the package does not do anything crazy with the filesystem!
Download but don't install the debian package. Then run
$ touch clean_up.sh
$ chmod +X clean_up.sh
$ gedit clean_up.sh
In the file add the following:
#!/bin/bash
all=$(dpkg -c steam*deb | awk '{print $6}')
for item in $all; do
#echo "Checking $item"
item=$(echo $item | sed 's/^\.//g')
if [[ -d ${item} ]]; then
#echo "-is a directory. Skipping"
continue
fi
echo "Removing file ${item}"
sudo rm -f ${item}
done
Afterwards, save and exit gedit and run:
./clean_up.sh
which will remove all the files it statically drops on your system.

Resources