Problems running first shellscript - linux

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.

Related

I wrote a script that's supposed to make a directory. It works without sudo, but doesn't with it. THe script is required to be called with sudo

I'm supposed to make a script that backups some files from /etc into a directory
that I must create called backup-confs. The problem is that I must run the script with sudo, but when I do it doesn't create the directory. It works fine without, but I can't figure out why it doesn't work with sudo.
#!/bin/bash
mkdir /home/student/tema3-scripts-output/backup-confs 2>> err.txt
This version also doesn't work
#!/bin/bash
cd 2>> err.txt..
cd tema3-scripts-output 2>> err.txt
mkdir backup-confs 2>> err.txt
cd .. 2>> err.txt
cd tema3-scripts 2>> err.txt
Dont do it all in one, split it up.
Maybe is has problems with creating a directory and at the same time redirecting the output from ".../backup-confs"
try using absolute references sudo mkdir /home/some-new-dir
give permissions to file chmod 777 /home/some-new-dir
and a personal preference, use "copy": cp /home/student/some-config /home/some-new-dir/some-config
I see you are working with relative directory references (like cd tema... instead of cd /tmp/tema...). When running under sudo, you are changing user, which might mean that you are changing directory, therefore I would advise you always to work with absolute directory references.

How to give permissions for specific commands in linux

I am new to linux. I have a build.sh file which consists of a lot of mkdir commands and some rm commands. But as I have installed this new in my VB, each time I run the .sh file, it says "Permission Denied for creating directory" and fails.
So is there any way that I grant directory privileges to all users.
Can anyone help me with this
Add "sudo" in the beginning of the directory creation command i.e
sudo mkdir dir_name
The issue might be with the directory in which the mkdir command is being run.
Use the command ll or ls -l to check the directory permissions.
If your directory doesn't have write privilege for the current user, you can run
chmod -R u+w /path/to/directory
This might require you to use sudo if permission is denied.
If you want to enable it for all users, run
chmod -R ugo+w /path/to/directory
Alternatively, a quick fix would be to run the build.sh file as root
sudo /path/to/build.sh
However, this approach is not advised unless you always run it as root

Remove ".bash_aliases" with bash script

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.

How to access specific path in linux using shellscript

Let us consider an example,
scriptPath=/home/sharath/Downloads/Atollic_TrueSTUDIO_for_STM32_9.2.0_installer
In the above line of code, If user is "sharath" then he can access a file/folder same way if the user is different how can access that folder/file dynamically.
below is my shellscript(.sh file):
#!/bin/bash
set -eu
configLocation=/etc/atollic
scriptPath=/home/sharath/Downloads/Atollic_TrueSTUDIO_for_STM32_9.2.0_installer
family=STM32
arch=x86_64
version=9.2.0
configFile=${configLocation}/TrueSTUDIO_for_${family}_${arch}_${version}.properties
installPath=/opt/Atollic_TrueSTUDIO_for_${family}_${arch}_${version}/
mkdir -p /opt/Atollic_TrueSTUDIO_for_STM32_x86_64_9.2.0/
tar xzf ${scriptPath}/install.data -C /opt/Atollic_TrueSTUDIO_for_STM32_x86_64_9.2.0/
In last line of the script, ${scriptPath} is diffrent for diffrent user, how can handle in shell script.
Update 1:
if i use, ${USER} or ${HOME} or whoami which returns "root" ,
Here is my log:
tar (child): /root/Downloads/Atollic_TrueSTUDIO_for_STM32_9.2.0_installer/install.data: Cannot open: No such file or directory tar (child): Error is not recoverable: exiting now
Update 2:
Currently user in "root"
Use $HOME for the start of scriptPath, i.e:
scriptPath=${HOME}/Downloads/Atollic_TrueSTUDIO_for_STM32_9.2.0_installer
I tried with couple of way and finally i found with below solution-
Use below script for the
users
myuser=$(users)
echo "The user is " $myuser
Here users returns current user name.
Your script become:
#!/bin/bash
users
myuser=$(users)
set -eu
configLocation=/etc/atollic
scriptPath=/home/$myuser/Downloads/Atollic_TrueSTUDIO_for_STM32_9.2.0_installer
family=STM32
arch=x86_64
version=9.2.0
configFile=${configLocation}/TrueSTUDIO_for_${family}_${arch}_${version}.properties
installPath=/opt/Atollic_TrueSTUDIO_for_${family}_${arch}_${version}/
mkdir -p /opt/Atollic_TrueSTUDIO_for_STM32_x86_64_9.2.0/
tar xzf ${scriptPath}/install.data -C /opt/Atollic_TrueSTUDIO_for_STM32_x86_64_9.2.0/
Thanks for answered my question.
Dynamic_Path="/home/$(whoami)/$SCRIPT_PATH"
What is the Linux OS you are using?
You can simply use as below,
scriptPath=~/Downloads/Atollic_TrueSTUDIO_for_STM32_9.2.0_installer
where ~ refers to the home directory of the user. i.e. /home/sarath
One other way is to use it like below,
scriptPath="/home/whoami/Downloads/Atollic_TrueSTUDIO_for_STM32_9.2.0_installer"

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