Help with A script

 
Thread Tools Search this Thread
Homework and Emergencies Homework & Coursework Questions Help with A script
# 1  
Old 02-01-2012
Help with A script

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


2. Relevant commands, code, scripts, algorithms:
cp ,echo ,read ,if


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:
Mod Comment Please use next time code tags for your code and data

Last edited by vbe; 02-02-2012 at 07:40 AM..
# 2  
Old 02-02-2012
Quote:
Originally Posted by Aldaron47
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

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'
# 3  
Old 02-02-2012
Goodmorning and thanks for the fast response

I have to copy the first named file to three target files as simple as possible
# 4  
Old 02-02-2012
Code:
#! /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

# 5  
Old 02-02-2012
I found the solution for the problem
The following code was the correct for me

Code:
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


Last edited by methyl; 02-02-2012 at 01:09 PM.. Reason: please usr code tags
# 6  
Old 02-02-2012
Quote:
Originally Posted by Aldaron47
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
Could you please explain the above code? I'm so eager to learn shell scripting.
# 7  
Old 02-02-2012
Quote:
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.

Last edited by methyl; 02-03-2012 at 06:03 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

5 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to block first bash script until second bash script script launches web server/site?

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)
Discussion started by: siegfried
3 Replies

2. Shell Programming and Scripting

Shell script works fine as a standalone script but not as part of a bigger script

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)
Discussion started by: ektubbe
2 Replies

3. UNIX for Dummies Questions & Answers

Calling a script from master script to get value from called script

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)
Discussion started by: Raj Roy
2 Replies

4. Shell Programming and Scripting

Script will keep checking running status of another script and also restart called script at night

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)
Discussion started by: ketanraut
1 Replies

5. Shell Programming and Scripting

create a shell script that calls another script and and an awk script

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)
Discussion started by: magikminox
0 Replies
Login or Register to Ask a Question