LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-09-2010, 12:02 PM   #1
centralsulcus
LQ Newbie
 
Registered: Nov 2010
Posts: 2

Rep: Reputation: 0
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!

Last edited by centralsulcus; 11-09-2010 at 01:04 PM.
 
Old 11-09-2010, 12:15 PM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
Hi, welcome to LQ!

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


Cheers,
Tink
 
Old 11-09-2010, 12:22 PM   #3
sag47
Senior Member
 
Registered: Sep 2009
Location: Orange County, CA
Distribution: Kubuntu x64, Raspbian, CentOS
Posts: 1,850
Blog Entries: 36

Rep: Reputation: 455Reputation: 455Reputation: 455Reputation: 455Reputation: 455
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.

Last edited by sag47; 11-09-2010 at 12:37 PM.
 
Old 11-09-2010, 01:12 PM   #4
centralsulcus
LQ Newbie
 
Registered: Nov 2010
Posts: 2

Original Poster
Rep: Reputation: 0
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.
 
Old 11-09-2010, 02:56 PM   #5
Dralnu
Member
 
Registered: Jul 2005
Posts: 335

Rep: Reputation: Disabled
Minor change

Quote:
Originally Posted by sag47 View Post
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.
 
Old 11-09-2010, 03:51 PM   #6
sag47
Senior Member
 
Registered: Sep 2009
Location: Orange County, CA
Distribution: Kubuntu x64, Raspbian, CentOS
Posts: 1,850
Blog Entries: 36

Rep: Reputation: 455Reputation: 455Reputation: 455Reputation: 455Reputation: 455
Quote:
Originally Posted by Dralnu View Post
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.

Last edited by sag47; 11-09-2010 at 03:57 PM.
 
Old 11-09-2010, 11:45 PM   #7
Dralnu
Member
 
Registered: Jul 2005
Posts: 335

Rep: Reputation: Disabled
Quote:
Originally Posted by sag47 View Post
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash script problem with ftp session exiting the script early edomingox Programming 5 02-23-2010 06:39 AM
How to get full path to script file inside script itself? And in case of sym links? maggus Linux - Newbie 3 05-28-2009 09:40 AM
MySQL Updates With Null When Perl Script Run From Shell Script ThisGuyIKnow Programming 6 08-12-2008 10:56 AM
set variables in a bash script; ansi PS1 color script donnied Programming 4 11-21-2007 12:33 PM
i get an error message running php script inside a cgi script. repolona Linux - Software 0 02-22-2007 10:10 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 11:11 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration