How to copy multiple using shell script - bash

I am beginner in shell script i am trying to copy multiple files. I want manually give the input how i change my code
echo "Please Enter File Name"
read b
cp -l ~/Downloads/"{$b,$b,$b,$b,$b}" ~/myProj/image

Not sure of what you mean by "I want manually give the input how i change my code."
But try this :
echo "Please Enter File Name"
read b
for i in $b
do
cp -l ~/Downloads/$i ~/myProj/image
done

Related

How to change a files permissions through a prompt using bash scripting

just started to learn how to script and Im trying to make a loop script which will automatically change the permissions of the requested file and ask the user again if they want to change another files permissions. So far this is what I have although I'm not entirely sure how to fully make the loop to work.
#!/bin/bash
until "$input"=no
do
echo "Enter the name of file to change permissions"
read filename
chmod 777 $filename
echo "$filename permissions has been changed"
echo "Would you like to change the permissions of another file?(yes or no)"
read $input
done
echo "You typed: $input"
Thanks to oguz's anwser I solved my problem, Use [ "$input" = 'no' ] instead of "$input"=no, quote $filename and drop that $ from read $input. Or just go read a tutorial on loops and conditional constructs

How to get a list of subdirectories from a file and then create those subdirectories in a directory?

When a user inputs a name, there should be a new directory that gets created under that name.
In addition to that, the script needs to consult a file structure1.txt which is found in /etc/scriptbuilder/str1.
In this file, it will list two subdirectories (one on each line), the script is then supposed to create these two subdirectories in the new directory the user just made and named.
So how can the script then create each of the subdirectories that are listed in this text file?
I'm completely lost on that part.
This is my code so far:
echo "Enter the project name "
read name
echo $name
if [ ! -d $name ] then
mkdir $name
else
echo "The project name you entered already exists"
fi
cp /etc/scriptbuilder/str1/structure1.txt /$name
#I know this is wrong
because this would just copy the file over to the new directory but not actually
make the two subdirectories that are on the file onto the new directory
The bash command that you are looking for is read.
Also the syntax for your if [ ! -d "$name" ] should have a semicolon.
The else would typically have an exit 1 (or some such value).
Typical bash code gets input from the command line, but what you want is fine.
For testing purposes, I inserted a ~ (tilde), which references your home directory.
The script should look something like:
filename="/etc/scriptbuilder/str1"
read -p "Enter the project name " name
echo "$name"
if [ ! -d ~/"$name" ]; then
mkdir ~/"$name"
else
echo "The project name you entered already exists"
exit 1
fi
while read -r line; do
mkdir ~/"$name/$line"
done < "$filename"
You can clean up the formatting.

using bash script to Input in word in a command

I am trying to make a simple bash script to run nano command via bash so that I don't need to type nano filename every time, I just execute ./n and I will be asked for enter file name and I type the file name and done, the file should open using nano.
I'm trying this but it is somewhere wrong,
#!/bin/bash
echo -n "Enter file name: "
read INPUT
echo "$INPUT" >> "$JD"
nano JD
Solve is very simple
this script do it:
#!/bin/bash
echo -n "Enter file name :"
read FileName
touch $FileName
nano $FileName
save as the "nanoScript.sh" like
and open terminal and write :
chmod +x nanoScript.sh
and you can this : ./nanoScript.sh
it will create file and open with nano

Where does if look for files in BASH

I'm wondering how to control where my if statements find files.
The code if [ -e filename ] should find a file, but where is it looking?
I have the following code I'm having trouble with:
#!/bin/bash
#create a new file
echo "Enter the customer data"
read -p "Email:" cemail
read -p "Name:" cname
read -p "Apt:" capt
read -p "Monthly Rent:" crent
read -p "Rent Due Date:" cdatedue
echo "$cemail"
#if its already a file then error
if [ -e "$cemail" ]
then
echo "Error: customer already exists"
exit 1
else
echo "ok"
fi
when I type in a filename that does exist it still isn't finding it. Our programs are in the
:~/courses/cs3423/2017Fa/Proj1 folder and the files we are searching against are in the
:~/courses/cs3423/2017Fa/Proj1/Data folder. And this is how the program has to be set up as well. How can I manipulate the if to function correctly, or do I need to use something else? Thanks.

2nd loop inside the script or asking to enter value

I have written a code but I am having a problem to make the double loop in my bash script. This script should read all the files 1 by 1 in the given directory to upload but the value of "XYZ" changes for each file. Is there a way for me to make the code ask me to enter the "XYZ" every time it reads a new file to upload? (if possible with the name of the file read) like "please enter the XYZ value of 'read file's name'" I could not think of any possible ways of doing so. I also have the XYZ values listed in a file in a different directory so maybe can it be called like the do loop I did for the path? I might actually need to use both cases as well...
#!/bin/bash
FILES=/home/user/downloads/files/
for f in $FILES
do
curl -F pitch=9 -F Name='astn' -F
"path=#/home/user/downloads/files;$f" -F "pass 1234" -F "XYZ= 1.2" -
F time=30 -F outputFormat=json
"http://blablabla.com"
done
try following once.
#!/bin/bash
FILES=/home/user/downloads/files/
for f in $FILES
do
echo "Please enter the name variable value here:"
read Name
curl -F pitch=9 -F "$Name" -F
"path=#/home/user/downloads/files;$f" -F "pass 1234" -F "XYZ= 1.2" -
F time=30 -F outputFormat=json
"http://blablabla.com"
done
I have entered a read command inside loop so each time it will prompt user for a value, since you haven't provided more details about your requirement so I haven't tested it completely.
The problem was actually the argument. By changing it to:
-F Name="$Name"
solved the problem. Trying to link the argument such as only $Name or "$Name" causes a bad reception.

Resources