Results 1 to 3 of 3
  1. #1
    Join Date
    Nov 2012
    Posts
    1

    seting variable = to value from .txt / cp with variable as argument

    I am trying to write a script that will copy all file listed in a text file (100s of file names) to a new directory each time I run it
    Essentially i wanna pick out certain files from a large collection to form a smaller on

    Im trying to work up a test script using this scaled down model

    Assume the script will run with main as current working directory and I know how many files/lines will be in List.txt

    Contents of main
    script.sh
    sort < is a folder
    List.txt
    stuff.pdf
    misc.rtf
    junk.txt
    thing.txt

    Contents of List.txt
    stuff.pdf
    misc.rtf
    thing.txt

    Script so far (contests of script.sh)
    #1/bin/bash

    count =0
    while [ $count -le 3 ]
    do
    filename= sed -n '1p' List
    cp $filename sort
    count=$(($count + 1 ))
    done

    Problems
    (1)
    count though lines with counter
    something like
    filename= sed -n '$countp' List

    Im open to useing a different command here cat or whatever
    but I want it to take the value from $count and grab whatevers is on the coresponding line in List.txt and store the result in a variable


    (2)
    use the file name stored in $filename in copy command
    currenty gives error "cp: missing destination file operand after 'sort'"
    I know this means its not reading the file name from $filename and is using sort as target not the destination but I dunno how to fix.

    Any help would be appreciated

  2. #2
    Join Date
    Jun 2003
    Location
    Toronto, Canada
    Posts
    5,429
    Something like this?
    Code:
    head -3 List.txt | while read F; do test -f $F && cp $F sort/; done
    ---
    "It does not work" is not a valid problem statement.

  3. #3
    Join Date
    Jun 2003
    Location
    West Palm Beach, FL
    Posts
    2,670
    Quote Originally Posted by IAmTheGrass View Post
    I am trying to write a script that will copy all file listed in a text file
    . . . Etc . . .
    (1)
    count though lines with counter . . .

    (2)
    use the file name stored in $filename in copy command . . .

    Any help would be appreciated
    (1) Why do you need to count?
    (2) Try this:
    Code:
    cat list.txt|xargs -i! cp ! sort/
    The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •