how do i assigne out put of command as variable - bash

Hi i am very new to scripting. please My apologizes if i am pointing in wrong way.
I am trying to develop a script which take the backup of given path. Below is my script.
The problem I facing is that I am trying to assign a variable "s" to an command "mkdir" and it do not work. Please help me,how i can correct this syntax?
#!/bin/bash
# to back up the given folder "
i="`date | awk '{ print $1$2$4}'`"
echo " please enter the full path of folder you want to back up"
read foldern
echo " $foldern is of `du -sh $foldern`. Do you want to back up this folder"
echo "yes / no"
read ans
if [ $ans = yes ]
then
echo " enter the back up folder name"
read bpn
s=$(mkdir $bpn$i) # here I am trying to assign a variable "s" for out put of mkdir but dosent work Please help me #
echo $s
cp -R "$foldern" "$s"
else
echo "no back up is taken"
fi

mkdir does not create an output or print the directory it has created. Manually create the string instead:
s=$bpn$i
mkdir -- "$s"
echo "$s"
-- is an optional option-argument separator so files beginning with - is not misinterpreted as a bad option to mkdir.
Adding -p can also be helpful if you don't want mkdir to show an error if the directory already exists.
mkdir -p -- "$s"
Suggestion:
#!/bin/bash
# To back up the given folder.
date=$(date | awk '{ print $1$2$4}') ## Consider date=$(date '+%F-%T')
read -p "Please enter the full path of folder you want to back up: " folder_name
size=$(du -sh "$folder_name")
read -p "$folder_name is of $size. Do you want to back up this folder (Yes/No)? "
if [[ $ans == [yY][eE][sS] ]]; then
read -p "Enter the back up folder name: " backup_name
backup_name+=$date
echo "$backup_name"
mkdir -p "$backup_name" && cp -R "$folder_name" "$backup_name"
else
echo "No back up is taken."
fi

Related

Repeat through bash script based on user input

#sets dir name
echo "What is the name of the target?"
read targetName
#changes dir to desktop
mkdir -p ../Desktop/Notes
cd ../Desktop/Notes
#make working directory
mkdir $targetName
cd $targetName
mkdir "IPs" "SubDomains" "Screenshots" "NmapScans" "Notes"
I have been trying to wrap my brain around simple loops in bash. I have the following script I would like to ask a user for "targetName" to create some directories. After the directories are created I would like the script to ask the user if they would like to create another target, If Y/Yes loop back, if no then exit. I realize this is a fairly simple issue, new to bash and programming in general and I work best if I create the problem myself. Im 99% sure I need a if loop for this. Im just not sure how to break it up correctly. Thanks in advance!
See if that helps you:
#!/bin/bash
while true
do
IFS= read -p 'What is the name of the target? ' -r targetName
# sanity checks:
# * no empty input
# * no '/' in input
if [ ${#targetName} -eq 0 ] || [[ ${targetName} == */* ]]
then
echo 'error: invalid target name' 1>&2
continue
fi
# for now, just print what you'll be doing
printf 'mkdir -p \\\n'
printf ' %q \\\n' ~/Desktop/Notes/"$targetName"/{IPs,SubDomains,Screenshots,NmapScans,Notes}
read -p 'Do you wish to create an other target?[y/n] ' -n 1 yesno
echo
case "$yesno" in
[Yy]) continue;;
*) break;;
esac
done

chmod command is not working in shell script

I am writing a script that needs to create a file if an argument is passed to he script.
If no argument is passed then it will ask for fileName but it needs to have default permission as -rwx- --- - ---.
I am using this command chmod a=-rwx to remove all the default permissions and then i am using chmod u=+rwx to get the desired permission as stated but it is not working.
Can anyone help please?
#!bin\bash
if [ $#==0 ]; then
echo "Please enter a file name?"
read fileName
if [ -f $fileName ]; then
echo "File already exist! Opening for Editing"
sleep 3
nano $fileName
else
echo "File created with the name $fileName"
echo "Opening $fileName for editing "
sleep 3
echo "#!bin\bash" >$fileName
nano $fileName
fi
elif [ -f $1 ]; then
echo "File already exists with the name $1"
echo "Opening for editing"
sleep 3
nano $1
else
fileName="$1"
chmod a=-rwx $fileName
chmod u=+rwx $fileName
echo "File created with the name $filename"
echo "Opening $fileName for editing "
echo "#!bin\bash" >$fileName
sleep 3
nano $1
fi
Your chmod syntax is incorrect. The operation is either = to set the mode to a specific value, + to add modes, or - to remove modes; you can't combine them with =+ and =-.
You can perform multiple operations on a single file by separating them with ,.
So it should be:
chmod go-rwx,u+rwx "$fileName"
Another problem:
if [ $#==0 ]
should be
if [ $# -eq 0 ]
Use -eq for numeric comparisons, and spaces are needed around operators in shell conditions.
Third problem: You're doing the chmod before you create the file. Put it after
echo "#!/bin/bash" >"$fileName"
Fourth problem: #!bin\bash should be #!/bin/bash.
Finally, remember to quote your variables, in case they contain spaces.

Handling files with spaces in a bash selection menu

I am trying to make this script handle file with spaces in them. it is supposed show and execute the content of files in a directory. when I select a file with a space in it, bash fails with bash: foo: no such file or directory, What am I missing to make this handle files correctly
# /etc/skel/.bashrc
#Interactive shell detection
if [[ $- != *i* ]] ; then
# Shell is non-interactive. Be done now!
return
fi
#kv-bash (easy) var database & setup of info
echo "type 'menu' for a bash menu"
#done####################
#to easily launch crouton enviroments
addentry() {
cd ~/.sslm
echo "Name your menu entry."
read entry
sleep 1s
if [ -e "$entry " ]
then
echo "Error, Menu entry already exists"
addentry
else
echo "what do you want the entry to do?"
read entryexec
echo "$entryexec && menu"> ~/.sslm/"$entry"
echo "done"
cd ~/
fi
sleep 1s
}
###################
delentry() {
cd ~/.sslm
ls -x
echo "what entry do you want to delete?"
read del
rm "$del"
echo "the work has been done, he is dead"
}
###################
menu() {
clear
cd ~/.sslm
echo "-- simple shell launcher menu v1.o --"
# set the prompt used by select, replacing "#?"
PS3="Use number to select a file or 'exit' to leave: "
# allow the user to choose a file
select filename in *
do
# leave the loop if the user says 'stop'
if [[ "$REPLY" == exit ]]; then
cd ~/
break
fi
# complain if no file was selected, and loop to ask again
if [[ "$filename" == "" ]]
then
echo "'$REPLY' is not a valid number"
sleep 1s
continue
fi
# now we can use the selected file, trying to get it to run the shell
script
. $filename
# it'll ask for another unless we leave the loop
break
done
}
menu
also, this is on a chromebook, so there is no apt.
At this part:
script
. $filename
I just needed to change to . "$filename"
thx #PesaThe

if and else in bash not working

I can't figure out where I am messing this up but its killing me and the more I mess with it the worse it gets.
I am trying to get a directory from user and display it before asking for a search term within the directory. I want the user to have to specifically type "q!" to exit, or else it just starts from the beginning. What am I doing wrong? Thanks in advance!
#!/bin/bash
echo 'Enter directory name, pwd for present working directory, or q! for quit.'
read $dirName
echo $dirName
read -p "Press [Enter] key to continue..."
echo 'Enter part, or all of the filename'
read $fileName
echo $fileName
read -p "Press [Enter] key to continue..."
if [ "$dirName" = "q!"]; then
exit 0
else
ls -l $dirName
ls -a *$fileName*
fi
I want the user to have to specifically type "q!" to exit, or else it
just starts from the beginning
you need to use loop, and check the user input, only break the loop when user input "q!".
$ is not needed in read $dirName (and the other read too).
#!/bin/bash
while true
do
echo 'Enter directory name, pwd for present working directory, or q! for quit.'
read dirName
[ "x$dirName" == "xq!" ] && break
#do other stuff
done
#!/bin/bash
echo 'Enter directory name, pwd for present working directory, or q! for quit.'
read dirName #<--- remove $
echo $dirName
read -p "Press [Enter] key to continue..."
echo 'Enter part, or all of the filename'
read fileName #<--- remove $
echo $fileName
read -p "Press [Enter] key to continue..."
if [ "$dirName" = "q!" ]; then #<--- add space after "q!"
exit 0
else
ls -l $dirName
ls -a *$fileName*
fi
This works in my pc.

performing multiple backups at the same time with a bash script

Create a script to backup a file or directory tree by making a zip of the file(s) and copying it $HOME/Backups. The zipfile name should include what it is backing up, and the date the file was created. The script should take a random number of arguments specifying what to backup. If it is not given at least one item to include in the backup, it should complain. Ive got most of it to work but im having issues with multiple files ie file1 file2 to backup at the same time
#!/bin/bash
clear
echo
echo "Use this script to backup files to your home/backups directory"
echo
ls -la
echo
echo "================================================================"
echo
echo -n "Input file(s)/dir to backup: " ; read filez
while [ "$filez" == "" ] ; do
echo -n "You didnt input a filename, try again: " ; read filez
done
while [ ! -e "$filez" ] ; do
echo -n "No such file/dir, try again: " ; read filez
while [ "$filez" == "" ] ; do
echo -n "You didnt input a filename, try again: " ; read filez
done
done
echo
echo "================================================================"
echo
echo -n "Input name of backup file you wish to create(date automatically included): " ; read filezname
while [ "$filezname" == "" ] ; do
echo -n "You didnt input a filename, try again: " ; read filezname
done
zip -r $HOME/backups/$filezname"_$(date +%F)" $filez
Personally, I'm a fan of bash scripts taking arguments right from the command line:
script arg1 arg2 arg3 ...
Bash takes arguments using the special array $#, and you can do arguments processing with shift. Something like this:
#!/bin/bash
filezname=$1
shift
filez=""
for file in "$#"
do
filez="$filez $file"
done
if [[ $filez == "" ]]
then
echo "Give me argz! Nom nom nom!"
exit 0
fi
# Do stuff
What this does is it takes the first argument, takes it as the name of the zip file, and then slurps the rest of the filenames you want to zip up into a big long space-separated string that you can play with.

Resources