Noob's 1st...bash-script for copying one file into many


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Noob's 1st...bash-script for copying one file into many
# 8  
Old 06-28-2010
OK so I figured out what each part of that little script in the for loop (along with why you use curly brackets when calling a variable)

a="${temp%-*}" ##cuts everything after the c off of the name of temp b/c using remainder command

a="${a%.*}" ##cuts the ".c" part.....though I'm not sure why this was needed since you could've just cut it in the first assignment of a by cutting before the "-"

a="${a}.1-${var}.${temp##*.}" ##calls for a adds "a .1-" then each of the variables {A B C D} but the last part has be stumped

how does ${temp##*.}" work ?

thanks again
# 9  
Old 06-28-2010
'man ksh' yields the following:
Code:
     ${parameter%word}
           Remove Smallest  Suffix  Pattern.  The  word  will  be
           expanded to produce a pattern. The parameter expansion
           then will result in parameter, with the smallest  por-
           tion of the suffix matched by the pattern deleted.

     ${parameter%%word}
           Remove  Largest  Suffix  Pattern.  The  word  will  be
           expanded to produce a pattern. The parameter expansion
           then will result in parameter, with the  largest  por-
           tion of the suffix matched by the pattern deleted.

     ${parameter#word}
           Remove Smallest  Prefix  Pattern.  The  word  will  be
           expanded to produce a pattern. The parameter expansion
           then will result in parameter, with the smallest  por-
           tion of the prefix matched by the pattern deleted.

     ${parameter##word}
           Remove  Largest  Prefix  Pattern.  The  word  will  be
           expanded to produce a pattern. The parameter expansion
           then will result in parameter, with the  largest  por-
           tion of the prefix matched by the pattern deleted.

Given the above description, try to work out the mechanics with a sample input.
This User Gave Thanks to vgersh99 For This Post:
# 10  
Old 06-28-2010
a little shorter:
Code:
temp=~/file.a.b.c-d.r
for var in {A..D}; do
   echo "cp $temp ${temp%?-*}1-$var${temp#*-?}"
done

# 11  
Old 06-29-2010
Ah nice! I think I can probably benefit from the long code since it make me think about the nuts and bolts. This is a steep learning curve. Thanks.
# 12  
Old 06-29-2010
You're definitely better off learning the long way, but here's a shorty...
Code:
tee file.a.b.{A,B,C,D}.r < file.a.b.c-d.r

Or, same thing but slightly longer and easier to understand:
Code:
cat file.a.b.c-d.r | tee file.a.b.{A,B,C,D}.r

The shell expands the things in curly brackets so
Code:
file.a.b.{A,B,C,D}.r

becomes the 4 files
Code:
file.a.b.A.r
file.a.b.B.r
file.a.b.C.r
file.a.b.D.r

and tee splits the output of the original file to all the files listed. The second case uses cat to display the file and pipe it to tee, the first case just uses the contents of the file as input for tee.
This User Gave Thanks to fubaya For This Post:
# 13  
Old 06-29-2010
Cool!...somehow pipe fitting is more intuitive to my biologist brain, than figuring out prefix and suffix patters...
# 14  
Old 06-29-2010
yes, really cool
but the output filenames are hard coded.

{k,ba,}sh can do it "softly"
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Bash to append array value to file before copying

The bash stores each uniqueid in an array and then passes them to %q to get the unique path. That seems to work what I am having trouble with is renaming each .png with the unique value in %q. I thought it was working but upon closer inspection, a .png file is being sent to scp.... but only 1 and... (21 Replies)
Discussion started by: cmccabe
21 Replies

2. Shell Programming and Scripting

Total Noob BASH scripting question

Hello All, I have a file of ip addresses called activeips.txt What I'm trying to do is run a simple bash script that has a loop in it. The loop is a cat of the IP addresses in the file. The goal is to run 2 nmap commands to give me outputs where each address in the list has an OS... (11 Replies)
Discussion started by: Dirk_Pitt
11 Replies

3. Shell Programming and Scripting

Dialog script for copying a file

Hello, I'm trying to write a script in Dialog for configuring a router. I basically need the user to select from a list of say 4 options, confirm the option and then copy the configuration files, depending on the choice to the HDD and then reboot (I'm hoping to boot from a USB stick). The... (0 Replies)
Discussion started by: pm77
0 Replies

4. Shell Programming and Scripting

Need help in finding and copying list of files using bash shell script

Dear All, I have a situation where I want to copy some files of type .txt. These files are o/p from one program. Some of the files are named as fileName .txt instead of fileName.txt after fileName by mistake I have specified "space". Now I want to move these files as follows. mv fileName*... (13 Replies)
Discussion started by: linuxUser_
13 Replies

5. Shell Programming and Scripting

Copying large files in a bash script stops execution

Hello, I'm new to this forum and like to first of all say hello to everyone. I've got a really annoying problem at the moment. I'm trying to rsync some files (about 200MB with one file of 120MB) from a Raspberry PI with raspbian to a debian server via rsync. This procedure is stored in a... (3 Replies)
Discussion started by: wex_storm
3 Replies

6. Shell Programming and Scripting

Copying a string from a file using shell script

Hello everyone I am completely new to shell scripting in linux. I wan to write a script to search for a certain string from a .txt file and copy the string which apears just after tat searched string. Eg: in a file- try.txt , we have a line saying: "roses are red, so what do i do" I... (4 Replies)
Discussion started by: Kishore920
4 Replies

7. Shell Programming and Scripting

Bash Script Issues (If statement for file copying)

Writing a bash script for use with Geektool, pulls the battery info, and shuffles images around so that an Image geeklet can display the correct expression as the desktop background. (Eventually I intend to make it more intricate, based on more variables, and add more expressions) I'm extremely... (1 Reply)
Discussion started by: The_Ardly374
1 Replies

8. UNIX for Dummies Questions & Answers

script copying the directory (or file) from server to my pc

Hello, I'm trying to create the shell script that: copy (or transfer) the directory from the unix server to my external hard drive (or hard drive) I've been serching this kind of thread here, but no luck so far. anyone can help me? Thank you. (2 Replies)
Discussion started by: myjwjw
2 Replies

9. UNIX for Dummies Questions & Answers

Having problems copying a file from a script

Hello. Complete newbie over here, and I'm hoping you can help me out with this problem. The script copies a file to a directory within my home dir. Permissions are ok and the source file exists. If I execute the cp command from the command line or hardcode the path/file name, it works. ... (6 Replies)
Discussion started by: verdugan
6 Replies

10. Shell Programming and Scripting

noob. need help to create a script.

Hi All. im a noob to scripting. could somone help me with a script please. what i want to do is. 1. run a cmd in the script - qmqtool -s this will give me an output similar to this. Messages in local queue: 790 Messages in remote queue: 306 Messages in todo queue: 23 i then want... (1 Reply)
Discussion started by: aron
1 Replies
Login or Register to Ask a Question