1. The problem statement, all variables and given/known data:
I have to make a script that will copy the contents of a already created folder to 3 others that are not created yet. The problem is that I cant create them so i get an error message for the folders that dont exist. If the total files are not 4 display an error message
3. The attempts at a solution (include all code and scripts):
Code:
if ( test $# -eq 4)
echo "Enter 4 files"
read $1 $2 $3 $4
then
touch $2
touch $3
otuch $4
cp $1 $2
cp $1 $3
cp $1 $4
else echo " Need exactly four parameters, sorry. "
fi
4. Complete Name of School (University), City (State), Country, Name of Professor, and Course Number (Link to Course):
PAPI , Chania ,Greece, Varos CS1111
Moderator's Comments:
Please use next time code tags for your code and data
if ( test $# -eq 4)
echo "Enter 4 files"
read $1 $2 $3 $4
then
touch $2
touch $3
otuch $4
cp $1 $2
cp $1 $3
cp $1 $4
else echo " Need exactly four parameters, sorry. "
fi
1. The lines which I've coloured red are contradictory. $1, $2, $3.... are positional parameters. You can't read values into them that way.
2. Your requirement was to create directories, not files, right? touch is used to change a file's access and modification time. If file doesn't exist, it creates them.
3. Try the below script. Let us know if you have any questions.
4. Please use code tags.
Code:
$ cat test.sh
#! /bin/bash
echo "Enter 4 dir names (1st is source. Next 3 are target dirs): "
read a b c d
for x in $b $c $d
do
if [ ! -d $x ]
then
mkdir $x
cp -r ./$a/* ./$x/
else
cp -r ./$a/* ./$x/
fi
done
$
$ ./test.sh
Enter 4 dir names (1st is source. Next 3 are target dirs):
test test1 test2 test3
$
$ ls -l
drw-r--r-- 3 root root 4096 Jan 24 06:27 test
drwxr-xr-x 3 root root 4096 Feb 2 04:55 test1
drwxr-xr-x 3 root root 4096 Feb 2 04:55 test2
drwxr-xr-x 3 root root 4096 Feb 2 04:55 test3
'test' was an already existing dir. test1, test2, test3 are newly created dirs, which contains all of the data contained in 'test'
#! /bin/bash
if [ $# -ne 4 ] # If number of positional parameters are not equal to 4,
then # Print warn msg and exit.
echo "Need exactly 4 parameters. Exiting."
exit
fi
for x in $2 $3 $4
do
if [ -e $x ] # If target file exists, delete it.
then # Even otherwise, cp would overwrite it.
rm -f $x # Some systems have an alias for cp as "cp -i".
fi # So cp would prompt the user if the target file needs can be over-written.
cp $1 $x # Same thing is achieved here by deleting and creating a fresh copy.
done
How to use:
Code:
$./test.sh src.txt tgt1.txt tgt2.txt tgt3.txt
$
$ ls -l *.txt
-rw-r--r-- 1 root root 12 Feb 2 12:46 src.txt
-rw-r--r-- 1 root root 12 Feb 2 12:46 tgt1.txt
-rw-r--r-- 1 root root 12 Feb 2 12:46 tgt2.txt
-rw-r--r-- 1 root root 12 Feb 2 12:46 tgt3.txt
touch a1
if (test $# -eq 4)
echo " Enter 1 source file and 3 target files"
read a1 a2 a3 a4
then
cp $a1 $a2
cp $a1 $a3
cp $a1 $a4
else echo " Need exactly four parameters, sorry. "
fi
No point learning about this one.
The parameter count checking bit of the script does not work at all. if (test $# -eq 4) does nothing except execute an irrelevant "test" in a subshell and ignore the result (which is good I suppose because $# has value zero at that moment).
The script collapses in a heap of "cp" errors if you just press return in response to the question.
I'm new to utilities like socat and netcat and I'm not clear if they will do what I need.
I have a "compileDeployStartWebServer.sh" script and a "StartBrowser.sh" script that are started by emacs/elisp at the same time in two different processes.
I'm using Cygwin bash on Windows 10.
My... (3 Replies)
Hello all,
I am facing a weird issue while executing a code below -
#!/bin/bash
cd /wload/baot/home/baotasa0/sandboxes_finance/ext_ukba_bde/pset
sh UKBA_publish.sh UKBA 28082015 3
if
then
echo "Param file conversion for all the areas are completed, please check in your home directory"... (2 Replies)
I am trying to call a script(callingscript.sh) from a master script(masterscript.sh) to get string type value from calling script to master script. I have used scripts mentioned below.
#masterscript.sh
./callingscript.sh
echo $fileExist
#callingscript.sh
echo "The script is called"... (2 Replies)
I am using blow script :--
#!/bin/bash
FIND=$(ps -elf | grep "snmp_trap.sh" | grep -v grep) #check snmp_trap.sh is running or not
if
then
# echo "process found"
exit 0;
else
echo "process not found"
exec /home/Ketan_r /snmp_trap.sh 2>&1 & disown -h ... (1 Reply)
Hi guys
I have a shell script that executes sql statemets and sends the output to a file.the script takes in parameters executes sql and sends the result to an output file.
#!/bin/sh
echo " $2 $3 $4 $5 $6 $7
isql -w400 -U$2 -S$5 -P$3 << xxx
use $4
go
print"**Changes to the table... (0 Replies)