Sponsored Content
Top Forums Shell Programming and Scripting Change directory within a bash shell script Post 303028378 by bakunin on Sunday 6th of January 2019 06:07:52 AM
Old 01-06-2019
Quote:
Originally Posted by HikingLife
Bakunin, I had tried using absolute paths, as my statement has multiple steps in between, the value of 'file' is lost in each step.
I do not think that the value "file" is "lost", i just think that "file" at each step doesn't contain what you think it does. This is your code:

Code:
for file in "$fSource"/*.done; do file="${file%.*}"; file="${file##*.}"; cp "${file:0:31}"*.* "$fPrepped"/; done

The first thing is: do NOT write one-liners. It is hard to understand what is going on when. So first, let us reformat:

Code:
for file in "$fSource"/*.done ; do
     file="${file%.*}"
     file="${file##*.}"
     cp "${file:0:31}"*.* "$fPrepped"/
done

Next thing: make sure that each step really does what you think it does. Notice that i took the final manipulation out of the cp command for clarity:

Code:
for file in "$fSource"/*.done ; do
     echo " after for: $file"
     file="${file%.*}" ; echo "first manipulation: $file"
     file="${file##*.}" ; echo "second manipulation: $file"
     file="${file:0:31}" ; echo "third manipulation: $file"
     echo "cp ${file}*.* $fPrepped/"
done

Now run that and observe: does "$file" in every step really contain what you expect it to contain? I suppose it doesn't. It is not "lost" IMHO. To quote from your post #1:

Quote:
Originally Posted by HikingLife
Basically check for all files with name like CAT_TURNAROUND_201811120_075221_3.done in the folder ABC/DEF/*.done, then get all the files starting with just this part CAT_TURNAROUND_201811120_075221 from this directory /ABC/DEF/ copied to /ABC/EDG/
So let us assume the content of "$file" is "CAT_TURNAROUND_201811120_075221_3.done" or "/ABC/DEF/CAT_TURNAROUND_201811120_075221_3.done", depending on you using absolute pathes or not. Running the manipulations above would give:

Code:
file => CAT_TURNAROUND_201811120_075221_3.done
${file%.*} => CAT_TURNAROUND_201811120_075221_3
${file##*.} => CAT_TURNAROUND_201811120_075221_3
${file:0:31} => CAT_TURNAROUND_201811120_075221

As you can see the one manipulation in the middle does absolute nothing. It can't do anything, because you cut off any "." that is in a files name with your first manipulation! And the last manipulation depends entirely on the exact length of the filename. What would it do with, say, "HORSE_TURNAROUND_201811120_075221_3.done"?

Let us try again: you want to cut off the extension (".done") from the filename. Fine! Then just do so! You know from the for-loop that all the values of "$file" will have exactly ".done" as extension, so there is no need for ${file%.*}. Do it explicitly: ${file%.done}. The net manipulation doesn't do anything (and will never do anything, see above), so get rid of it. Now for the last one: it produces the correct result in your example but as i have shown you it might break with other values. Taking a step back: we want to cut off everything up to the last underscore ("_") from the filename. So let us do that: ${file%_*}.

Notice that we could have done so immediately without all the intermediary steps! This of course is only the case as long as the extension contains no underscore. So until you change your for-loop to something like for file in *.do_ne you can just skip all the steps and use just the last one i suggested.

Would the result be any different if we use absolute pathes? No, because we only cut off from the end where the filename is.

You might want to adapt your script accordingly.

I hope this helps.

bakunin
This User Gave Thanks to bakunin For This Post:
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Change of directory thru script

:confused: Hi All, This script is not working. I want to change the directory as per users selection in current shell. Looks like it is spawning sub-shell internally. I have used . changedir.sh source changedir.sh ./changedire.sh But did not work. Currently shell directory remain the... (4 Replies)
Discussion started by: shiningram
4 Replies

2. Shell Programming and Scripting

change directory using shell script

How do u change ur directory using a shell script?? (0 Replies)
Discussion started by: laxmi
0 Replies

3. Linux

Change directory in a shell script

How do u change ur directory using a shell script?? (12 Replies)
Discussion started by: laxmi
12 Replies

4. Shell Programming and Scripting

how to change shell from BASH to C

hi all, how can i change my shell from BASH to C shell? i am using cygwin. (3 Replies)
Discussion started by: npatwardhan
3 Replies

5. UNIX for Dummies Questions & Answers

Change the default shell from bash to ksh

Currently my default shell is bash.How can i change itto ksh... (2 Replies)
Discussion started by: dr46014
2 Replies

6. Shell Programming and Scripting

Automatically change to Bash shell after login

Hi men, I think this must be a very common job. "How could to Automatically change to Bash shell after login and then jail user can only using this shell". I want monitor user works.However it just only effect on Bash shell. Consequently if the user change the shell it will be worthless. ... (4 Replies)
Discussion started by: tien86
4 Replies

7. Shell Programming and Scripting

Simple BASH shell script to rename webcam jpg and copy into a new directory.

System: Ubuntu Intrepid Ibex I'm running webcamd as a sort of "security" program, but I need a script that will archive my webcam.jpg files. So, take the following file: /home/slag/www/webcam.jpg Rename it--preferably with a time stamp. Place it in say: /home/slag/www/history/ ... (4 Replies)
Discussion started by: robfindlay
4 Replies

8. Shell Programming and Scripting

directory change in shell script

Hi, I am trying to change the directory in my script, & want to check if the directory is present or not . Ex: cd /home/xyz/temp/logs if the logs directory is not present i want to show the error in script instead of shell script error. Can anybody please help me on the same Thx in... (2 Replies)
Discussion started by: vls1210
2 Replies

9. Shell Programming and Scripting

How to change a directory in shell script?

HI, I need to change the working directory by using the shell script /Export/home/user_name I have to go one step back like /Export/home Please help on this.:confused: (3 Replies)
Discussion started by: thelakbe
3 Replies

10. Shell Programming and Scripting

Change directory shell

#!/bin/bash echo -n "Enter number of sanger patients : "; read id perl -ne 'chomp; system ("perl table_annovar.pl $_ humandb/ -buildver hg19 -protocol refGene,popfreq_all,common,clinvar,clinvarsubmit,clinvarreference -operation g,f,f,f,f,f -otherinfo")' < file.txt I have the above script... (7 Replies)
Discussion started by: cmccabe
7 Replies
All times are GMT -4. The time now is 01:33 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy