Remove ".bash_aliases" with bash script - linux

In my .bashrc I'm using .sh script for easily configuring newly installed Debian. But while trying to
rm -f ~/.bash_aliases
wget https://raw.githubusercontent.com/.../.bash_aliases
rm -f ~/.bashrc
wget https://raw.githubusercontent.com/.../.bashrc
it's just omitting those line?
File is with permission chmod +x ./script.sh and run by sudo ./script.sh
What could possibly be wrong?
(In final code there is full link, files are being downloaded as .bashrc.1 and .bash_aliases.1)

Don't use sudo unless you have a good reason.
When you run sudo ./script.sh it runs as root, so ~ refers to root's home directory /root instead of your user's home directory.
Just run ./script.sh instead, so that it runs as you and modifies your own home directory.

Related

My script file stopped executing when there change in user in script what should i do

My code file looks as follows
Cd testR
Mkdir bin
chmod -R 755 bin
Sudo su - inst1
cp inst1/installable/files/testR.p
...
...
So after sudo su the execution get paused please let me know what should i do
If there is no problem that your commands are executing as root, you can execute your script like this:
sudo ./yourscript
So, you don't have to switch within your shell script.

Problems running first shellscript

I'm trying to create my first shell script in bash. I've created the code and I've managed to save the script in my home directory but it wont run. At first I try running it from the home directory with: ./testscript.sh with "permission denied" as a response, i then tried sudo ./testscript.sh and then the "command was not found".
This is my script:
#!/bin/bash
mkdir -p/home/filer
touch /home/filer/fil1
touch /home/filer/fil2
touch /home/filer/fil3
tar-zcvf file.tar.gz /home/filer
So I've tried creating a script that will create a directory called "filer" in my home directory, using touch to create 3 separate files within the "filer" directory and then creating a tar.archive out of the whole "filer" directory. I think the script is correct, I could just use a hand running the script.
Other than a couple of typos (mkdir -p/path -> mkdir -p /path, tar-zcvf ... -> tar -zcvf ...), you should refer to your home directory using $HOME environment variable. /home/filer is an absolute directory path, which I am assuming, is not your actual home directory.
#!/bin/bash
mkdir -p $HOME/filer
touch $HOME/filer/fil1
touch $HOME/filer/fil2
touch $HOME/filer/fil3
tar -zcvf file.tar.gz $HOME/filer
You can execute the script, ./testscript.sh as bash testscript.sh or ./testscript.sh.
In the second case, the script need to have proper executable permissions. chmod +x ./testscript.sh gives it full executable permissions.

Why do I need to sudo to use leiningen (installed in /bin) on Linux?

I installed leiningen on fedora, I followed the instruction:
download the script
Paste the script(via command line) to ~/bin (PATH)
Execute the script
Run lein(I had to did it with sudo) to self-install
but every time I want to run the "lein" command, I have to do it with "sudo".
How can i fix this? or what can i do to fix this?
Note: I installed leiningen at /bin but when i cd ~/bin as the installation guide said i get and error about the folder(not exist).
You installed it in /bin and ran it with sudo to install the lein jars initially, which means they are owned (and probably only readable) by root. You should install the script at ~/bin instead. You can fix it like this:
sudo rm /bin/lein
sudo rm ~/.lein
mkdir ~/bin
cd ~/bin
wget https://raw.githubusercontent.com/technomancy/leiningen/stable/bin/lein
export PATH="${HOME}/bin:${PATH}"
lein
You should also add ${HOME}/bin to your $PATH. If you are using bash, add this to ~/.bashrc:
export PATH="${HOME}/bin:${PATH}"
You can do that using echo:
echo 'export PATH="${HOME}/bin:${PATH}"' >> ~/.bashrc

How to create a custom bash command in linux

The question is about bash shell commands in ubuntu 10.04.
I have created a simple addition program in c and it works fine in my terminal.
Now I want to make this program to execute into my terminal as a command.
How can I convert a C program into a bash shell command?
How to make that command an system command like others?
yout just have to change it's owner & group root following commands
sudo chown root "file_name"
sudo chgrp root "file_name"
then give this command to change the permissions
sudo chmod 755 "file_name"
and place it in /bin with this command
sudo mv "file_name" /bin
now u can run it as a normal command.
You run your code by ./compiled-c-program
If you like to run like the other "system" program you need to add a static link to your program to one of the folder from your $PATH variable e.g.:
ln -s ~/bin/c-compiled-c-program path/to/the/program/compiled-c-program
Good luck!
I guess you want this C program to be executed by any user, as a system command. If this is your requirement, then you can add an execute permission to everyone by chmod +x <program name> and then add the program absolute path in the system define PATH environment variable.

How to downlaod a file in a directory using wget command in bash script?

I want to download a list of files using "wget" command of linux in a bash script file. The problem is that when I am trying to change the directory to another subdirectory in my home, it does not work and the wget after the cd command will download the files in my home directory not the desired subdirectory
mkdir -m 777 "dbback2012"
cd "dbback2012"
wget -r [FTP URL]
The problem is that the downloaded files via wget are in the home directory not the "dbback2012" directory.
There's nothing wrong with the code, you either
haven't shown us the real code
the script is executed somewhere else, check the working directory: pwd
the script failed to create the directory mkdir -m 777 "dbback2012" || (echo "ooops"; exit 1)

Resources