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! |
Hi, welcome to LQ!
The first thing I notice is that you keep using "&" instead of "$" where you try to access variables. Cheers, Tink |
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 Code:
./myscript.sh < ./subsetfile.txt Also what's the point of... Code:
if [ ! -d $PWD/subset_me ]; then You don't even use the subset_me/QA directory from the information you've given. |
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. |
Minor change
Quote:
You could also use a here doc instead of several echo statements like so Code:
#!/bin/bash |
Quote:
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. |
Quote:
|
All times are GMT -5. The time now is 06:15 PM. |