LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   script help (https://www.linuxquestions.org/questions/linux-newbie-8/script-help-843258/)

centralsulcus 11-09-2010 11:02 AM

script help
 
Hello everyone, I'm new around here (obviously) and have very limited coding experience. I took a java course 2 years ago, and have done very little with coding since. I have a question about a script I'm trying to create. First I'll type out the jist of the code I'm writing via emacs.

#!/bin/bash
DIR=`pwd`
procnums=('more $DIR/subsetfile.txt`)

if [ ! -d $DIR/subset_me ]; then
mkdir -p $DIR/subset_me/QA
else
echo "Either the original QA directory is not in X, or you've already run this script"
echo "You must delete the subset directory before this script will run"
fi
#This is where it should iterate through the text file numbers and copy all files to the new directory

for procnum in ${procnums[*]}; do
cp &DIR/QA/*${procnums}* $DIR/subset_me
done

Basically the text file I want it to read from has 10 lines, each line has a number. These numbers correspond to multiple files that all begin with that number, but have different extensions. I want the script to read the number, look into the original directory for any file that begins with that number, and copy those files into the newly created directory. It halts on the first number stating "No such file or directory", but the directory is intact as well as the files inside of it. I confirmed that all the files all there.

Any idea why it's dying on the first line in the text file?

If anyone could help me out, it'd be appreciated greatly. Thank you!

Tinkster 11-09-2010 11:15 AM

Hi, welcome to LQ!

The first thing I notice is that you keep using "&" instead of "$"
where you try to access variables.


Cheers,
Tink

sag47 11-09-2010 11:22 AM

Welcome,
Please use the "Go Advanced" in posts to format code into code tags and any other formatting which would make a post easier to read.

First off a few things I'd like to comment on (which may just be a typo on your part). Variables and their expansions start with $ and not &. You do not need to create a variable $DIR because the pwd echoes an already existing variable called $PWD.

Personally I think this would be a better way of reading your file and accomplishing your task. It makes better use of your $PWD variable. Lets call this script myscript.sh.

Code:

#!/bin/bash

if [ ! -d $PWD/subset_me ]; then
  mkdir -p $PWD/subset_me/QA
else
  echo "Either the original QA directory is not in X, or you've already run this script"
  echo "You must delete the subset directory before this script will run"
  #exit with an error code
  exit 1
fi
#This is where it should iterate through the text file numbers and copy all files to the new directory

while read line; do
  cp "$PWD/QA/$line*" "$PWD/subset_me/"
done

#successfully run script, exit
exit 0

The trailing forward slash in my cp command is important because it tells cp to copy the files into the directory and not as a file with the same name. I am quoting in the cp command to account for white space in path names. You can run the script with the following command.

Code:

./myscript.sh < ./subsetfile.txt
That's about as much advice as I can give you unless you provide samples of the contents of subsetfile.txt and a sample file structure this script will be working on.

Also what's the point of...
Code:

if [ ! -d $PWD/subset_me ]; then
  mkdir -p $PWD/subset_me/QA
else
  echo "Either the original QA directory is not in X, or you've already run this script"
  echo "You must delete the subset directory before this script will run"
  #exit with an error code
  exit 1
fi

?

You don't even use the subset_me/QA directory from the information you've given.

centralsulcus 11-09-2010 12:12 PM

Right, sorry about that. It was a type. Essentially the files rest in a folder called tbss.
Within tbss, there is QA, and a few other files. I want to create the subset folder within tbss, and create a new "QA" folder within subset, as I want this new directory to contain 10 of the 194 subject data files I have within the original tbss/QA. So that line in question will look in tbss, and create a folder called subset_me. I did this because I will likely be re-running this scripts many many times to look at different data.

So when the script finishes running, I want it to look like this. tbss/subset_me/QA. I only want this script to create subset directories, and copy the necessary files from tbss/QA to tbss/subset_me/QA.

So this subsetfile.txt has participant identifier numbers that identify various data files with different extensions that are all from the same participant (example: 1044, 1193, 3300 etc.). So each file I want copied will be starting out with one of those numbers, and will have different extensions.

Thanks for the tip, I'll rewrite this thing and try again.

Dralnu 11-09-2010 01:56 PM

Minor change

Quote:

Originally Posted by sag47 (Post 4153811)
Code:

#!/bin/bash

if [ ! -d $PWD/subset_me ]; then
  mkdir -p $PWD/subset_me/QA
else
  echo "Either the original QA directory is not in X, or you've already run this script"
  echo "You must delete the subset directory before this script will run"
  #exit with an error code
  exit 1
fi
#This is where it should iterate through the text file numbers and copy all files to the new directory

while read line; do
  cp "$PWD/QA/$line*" "$PWD/subset_me/"
done < $file

#successfully run script, exit
exit 0


Where $file is the file you are trying to read in. That should work, and saves some extra typing in the long run.

You could also use a here doc instead of several echo statements like so

Code:

#!/bin/bash

cat << EOF
Line 1
Line 2
Line 3
EOF

Also, I don't think you will need to use $PWD, since a script will run where it is called.

sag47 11-09-2010 02:51 PM

Quote:

Originally Posted by Dralnu (Post 4153957)
Where $file is the file you are trying to read in. That should work, and saves some extra typing in the long run.

You could also use a here doc instead of several echo statements like so

Also, I don't think you will need to use $PWD, since a script will run where it is called.

My method still works because it is reading the file from stdin. See this thread for an example. You're not wrong however. The reason why I chose to use stdin as input is because this method allows the OP to symlink the script in /usr/bin and use it in any directory as a built-in command. It's more modular.

There's a thousand ways to do the same thing.

Using $PWD is better because you never know how the script is being called. It's best not to assume anything since computers are stupid.

Dralnu 11-09-2010 10:45 PM

Quote:

Originally Posted by sag47 (Post 4153999)
My method still works because it is reading the file from stdin. See this thread for an example. You're not wrong however. The reason why I chose to use stdin as input is because this method allows the OP to symlink the script in /usr/bin and use it in any directory as a built-in command. It's more modular.

There's a thousand ways to do the same thing.

Using $PWD is better because you never know how the script is being called. It's best not to assume anything since computers are stupid.

All true, though I think it is only in perl that there is a thousand ways to do something. I think bash stops after a hundred.


All times are GMT -5. The time now is 06:15 PM.