UNIX Shell scripting | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

UNIX Shell scripting

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 02-15-2013
Rami Reddy Rami Reddy is offline
Registered User
 
Join Date: Feb 2013
Last Activity: 17 September 2013, 5:50 AM EDT
Location: Chennai
Posts: 20
Thanks: 15
Thanked 0 Times in 0 Posts
UNIX Shell scripting

Hi,

How to read .lst file line by line and while reading time it needs to copy the file into another file in unix shell script.

could you please help in this issue.

Thanks,
Rami Reddy.
Sponsored Links
    #2  
Old 02-15-2013
panyam panyam is offline Forum Advisor  
Registered User
 
Join Date: Sep 2008
Last Activity: 24 July 2014, 3:48 AM EDT
Posts: 1,156
Thanks: 20
Thanked 104 Times in 99 Posts
Welcome to the Forum.

Please post sample input and expected output.
Sponsored Links
    #3  
Old 02-15-2013
Rami Reddy Rami Reddy is offline
Registered User
 
Join Date: Feb 2013
Last Activity: 17 September 2013, 5:50 AM EDT
Location: Chennai
Posts: 20
Thanks: 15
Thanked 0 Times in 0 Posts
Hi,

This is sample script.

Code:
#!/bin/bash
#SCRIPT:  method2.sh
#PURPOSE: Process a file line by line with redirected while-read loop.

FILENAME=test.txt
count=0

while read LINE
do
        let count++
        var=$LINE
        cp $Line C:\Users\25267\Desktop\ram\

      echo "$var"
      cp $var C:\\Users\\25267\\Desktop\\ram\\



done < $FILENAME

echo -e "\nTotal $count Lines read"

Thanks,
Rami Reddy

Moderator's Comments:
Use code tags, thanks.

Last edited by zaxxon; 02-15-2013 at 07:09 AM.. Reason: code tags, see PM
    #4  
Old 02-15-2013
panyam panyam is offline Forum Advisor  
Registered User
 
Join Date: Sep 2008
Last Activity: 24 July 2014, 3:48 AM EDT
Posts: 1,156
Thanks: 20
Thanked 104 Times in 99 Posts
still not sure what exactly you need.!!!

where does $Line in your script comes from?

Assuming that the file content is a file name, something like this should be enough. I assume that destination folder is : /tmp


Code:
for file_name in `cat  test.txt`
do
cp $file /tmp
done

The Following User Says Thank You to panyam For This Useful Post:
Rami Reddy (02-15-2013)
Sponsored Links
    #5  
Old 02-15-2013
bakunin bakunin is offline Forum Staff  
Bughunter Extraordinaire
 
Join Date: May 2005
Last Activity: 31 July 2014, 8:54 AM EDT
Location: In the leftmost byte of /dev/kmem
Posts: 4,130
Thanks: 44
Thanked 775 Times in 610 Posts
Your sample script is wrong on so many points i don't even know where to start:


Code:
#!/bin/bash
#SCRIPT:  method2.sh
#PURPOSE: Process a file line by line with redirected while-read loop.

FILENAME=test.txt
count=0

while read LINE
do
        let count++
        var=$LINE
        cp $Line C:\Users\25267\Desktop\ram\

      echo "$var"
      cp $var C:\\Users\\25267\\Desktop\\ram\\

done < $FILENAME

echo -e "\nTotal $count Lines read"

1)

Code:
let count++

This might have been fashionable in the seventies, todays shell all have integer arithmetic built-in and therefore:


Code:
(( count++ ))


2)

Code:
var=$LINE

Letting aside that i fail to see the gain in copying "$LINE" to "$var" in your script (you could use $LINE directly, no), this line will only work correctly if "$LINE" contains no whitespace. Try the following and notice the error:


Code:
x="abc def"
y=$x
echo $y

correct is to quote such a statement:


Code:
var="$LINE"


3)

Code:
cp $Line C:\Users\25267\Desktop\ram\

First, "$Line" is nowhere defined in your script. Note that "$Line" and "$LINE" are two different variables because UNIX IS CASE SENSITIVE!

Second: It seems you are trying to pass a path as argument to "cp", but this is a DOS-path (or maybe a Windows-path), but not a UNIX-path. The backslash "\" is used to declare that the next character is to be taken literally and none of its special functions should be used. For instance, you could do:


Code:
cp file\* /some/where

and now "*" would be taken literally. That is, not all files starting with "file" will be copied but the one file named "file*".

So the file/path you are trying to copy to is named: C:Users25267Desktopram^J because you escaped with the last backslash the line feed, which probably followed the command. (The "^J" is the display value of a line feed.) While this is a valied file name for UNIX it is perhaps not what you had in mind.

4)

Code:
cp $var C:\\Users\\25267\\Desktop\\ram\\

This is the same as in 2) (the quoting is missing) and more of the problems i explained in 3). This time, though, you used the backslashes to escape other backslashes and after the shell is done with your code it really looks like a DOS-path. This is still not understandable to UNIX.

5)
Your script sets no exit explicit value. While this is legal it is bad practice and you should not even start to work this way.

I hope this helps.

bakunin
The Following 2 Users Say Thank You to bakunin For This Useful Post:
Rami Reddy (02-15-2013), sadikkilic (02-15-2013)
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
UNIX shell scripting lokita jain Shell Programming and Scripting 1 12-05-2012 05:42 AM
Unix Shell Scripting dw15 UNIX for Dummies Questions & Answers 3 08-14-2012 05:11 PM
Unix Shell Scripting( Calling from Unix to PLSQL) faizsaadq UNIX for Dummies Questions & Answers 1 07-23-2012 11:41 AM
Need your Help on Unix Shell Scripting......... vinayraj UNIX for Advanced & Expert Users 5 02-08-2008 06:00 AM
difference between AIX shell scripting and Unix shell scripting. haroonec Shell Programming and Scripting 2 04-12-2006 08:12 AM



All times are GMT -4. The time now is 05:38 PM.