KSH Script -- loop and data copy question


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting KSH Script -- loop and data copy question
# 1  
Old 05-10-2011
KSH Script -- loop and data copy question

I am trying to write a script that will allow me to train others with commands that I run manually by only allowing the exact command before continuing onto the next set of commands. Here is where I come into an issue. I have changed the directories for this post.

Software we run creates files stored in /export/home/test. I need to copy them from this directory to /export/home/copy_from_test. There are more than one file with the same few letters being the same. (i.e. CF_test; CF_test1; CF_test2) I would like all of the files to be copied with 1 command
Code:
cp -p /export/home/test/CF* /export/home/copy_from_test/

The problem I am having is if the trainee doesn't include the * in the command then it only copies the first file and continues through the rest of the script. I need it to error and loop back to the part where the command is to be entered.

The script is currently setup like so
Code:
echo
while [ "forever" ]
do
echo "Type the first 3 letters of the files to be copied here: \c"
read COPYFILE
if [[ $COPYFILE = "/export/home/test/$COPYFILE* ] && [ "${COPYFILE}" ! = "" ]];
then
     break
fi
done
echo
     while [ "forever" ]
     do
     echo "Enter Command"
     read CMDLINE
     if [[ $CMDLINE = "cp -p /export/home/test/$COPYFILE* /export/home/copy_from_test/" ]];
     then
            break
     fi
     done
            if [[ $CMDLINE = "cp -p /export/home/test/$COPYFILE* /export/home/copy_from_test/" ]];
           then
                  $CMDLINE
           fi
           done
echo
echo "command complete."

Any help is appreciated. Thanks agian.

Hurtz
# 2  
Old 05-10-2011
Something like this?
Code:
 while ! [[ $CMD =~ '*' ]] ; do 
   echo "Input command: "
   read CMD; 
done

Your code has some flaws, some of them worse than others. There are some syntax errors too. This cannot work:
Code:
if [[ $COPYFILE = "/export/home/test/$COPYFILE* ] && [ "${COPYFILE}" ! = "" ]];

Keep in mind, that when you define a variable with '*', you have to enclose it in single quotes to prevent shell globbing.
Code:
sh$ ls
file1
file2
file3
sh$ var="fi*"
sh$ echo $var
file1 file2 file3
sh$ var='fi*'
sh$ echo $var
fi*

This User Gave Thanks to mirni For This Post:
# 3  
Old 05-10-2011
Applying lateral thought it is probably not necessary for the script user to enter the trailing asterisk. We can hard code it into the script. Also by using "find" to generate a list of files we can process each file one-by-one and eliminate most of the potential issues with wildcards in shell scripts.

Code:
echo
while true
do
        echo "Press Return to exit"
        echo "Type the first 3 letters of the files to be copied here: \c"
        read PREFIX
        if [ "${PREFIX}""X" = "X" ]
        then
                break
        fi
        find /export/home/test -type f -name "${PREFIX}*" -print | \
        while read FILENAME
        do
                echo "Copying: ${FILENAME}"
                cp -p "${FILENAME}" /export/home/copy_from_test
        done
        echo
done
echo
echo "command complete."

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Creating a ksh script to copy a folder

Hi Folks, I need help in creating a ksh script to copy a folder from one server to multiple other servers using ssh and sftp. The thing is the folder name changes daily. So is there a way we could specify the folder name during the execution of script.? I would appreciate a quicker help.... (3 Replies)
Discussion started by: lena07
3 Replies

2. Shell Programming and Scripting

Help with loop in ksh script

Hi, I am new to UNIX. I am working on a script where it takes the input and produces a desired output and it works fine for one instance. Input(One Instance): CREATE TABLE TAB1 ( COL1, COL2 ); CREATE UNIQUE INDEX XPKTAB1 ( COL1 )TAB1; Output: CREATE TABLE TAB1 ( COL1, COL2... (8 Replies)
Discussion started by: varun2327
8 Replies

3. UNIX for Dummies Questions & Answers

UNIX ksh Copy Files Script

I need a UNIX ksh script that counts the number of files in directory, if the files exceed 20 files, then email results. I want the script to run every hour.. I don't have access to cron.. I'm some what new to UNIX. Windows guy all my career.. this is what I have so far.. #!/bin/ksh # count.sh ... (5 Replies)
Discussion started by: PerlHaven2k
5 Replies

4. Shell Programming and Scripting

Aix .ksh for loop script.

Hi, I'm trying to write a for loop to run through a list of servers and for each server copy a file to a backup file. But I can't seem to get it to run through my server list. It work for individual servers, please see below. #!/bin/ksh SSH_USERID=khcuser webservers="server1 server2" ... (2 Replies)
Discussion started by: elmesy
2 Replies

5. Shell Programming and Scripting

explain while loop in ksh shell script

#!/bin/ksh log=ABCl log=EFG log=HIJ i=0 while <------ what is the meaning of ($i - lt 3) do print ${log} (( i=i+1 )) done (1 Reply)
Discussion started by: Bperl1967
1 Replies

6. Shell Programming and Scripting

Setting a variable in a while loop (.ksh script)

Hello Everyone, I'm still trying to grasp many concepts in .ksh scripting, one of them being variables inside loops. My problem is the following: * I'm trying to set a variable inside a while read loop to reuse it outside of said loop. My lines are the following :... (13 Replies)
Discussion started by: jimmy75_13
13 Replies

7. Shell Programming and Scripting

for loop in awk script using ksh

Guys, I am new in awk , I face problem while i try to use for loop in awk, I am using ksh, i am trying to set a for loop which runs as man times as the records in a file , the for loop like for(a=1;a<=5;a++) is working in my awk script but the one i need is not working :wall: for example ... (8 Replies)
Discussion started by: djahmed
8 Replies

8. Shell Programming and Scripting

Script to copy specific data.

Hi, I have written a shell script which monitors my application and generates a report. The out put of the same looks like this. PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 30700 xmp 15 0 2986m 2.9g 1488 S 0.0 18.3 66:00.93 cee This report is generated... (8 Replies)
Discussion started by: Siddheshk
8 Replies

9. Shell Programming and Scripting

Solaris KSH shell script to copy all lines from one file to another

Hello, more of a windows wscript guy. However I took a new position that requires me to support some solaris servers. So... issue is that I need to copy all lines from a file to a temporary file and then copy them back into the original file starting at line 1. Reason I need to do this is... (5 Replies)
Discussion started by: ZigZaggin
5 Replies

10. Shell Programming and Scripting

Sample ksh script for copy the data from excel to database table ?

Hi All, I need to convert the data from excel to database table in sybase. Please provide some sample script.. thanks, Royal. (1 Reply)
Discussion started by: royal9482
1 Replies
Login or Register to Ask a Question