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.

Welcome to the Forum.

Please post sample input and expected output.

Hi,

This is sample script.

#!/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

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

for file_name in `cat  test.txt`
do
cp $file /tmp
done
1 Like

Your sample script is wrong on so many points i don't even know where to start:

#!/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)

let count++

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

(( count++ ))

2)

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:

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

correct is to quote such a statement:

var="$LINE"

3)

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:

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)

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

1 Like