I have been trying to execute the below command by changing directory and then copying contents of one directory to another by doing some file name manipulations in between. However this isnt working since as soon as the statement completes it goes back to the original folder. Can someone help in telling me how to change a directory in a script so I can execute subsequent steps within it using and modifying a variable through the steps.
This works fine from command line, however gives bad substitution error from the script because of the issue explained above
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_075221from this directory/ABC/DEF/ copied to /ABC/EDG/
Will really appreciate a solution. I have been trying to figure this out and checked various forums and tried alias, function etc. But it isn't working.
Moderator's Comments:
Please use CODE tags as required by forum rules!
Last edited by HikingLife; 01-05-2019 at 10:03 PM..
Reason: Correcting typos and fixing the sample file name
Regarding the permanent "cd":
if you run a script like a program then it cannot change anything in the calling shell.
But you can source it
or
This will also work around the error you have got. The error you could also avoid by running the script with
or changing its shebang (first line) to #!/bin/bash
Not sure I understand the problem. Would you please carefully proofread your post and correct typos and logical errors (using the "edit" button at the lower right)? Like the ABC?DEF/*.done - is that a (wildcarded) directory or a sequence of files?
You don't seem to cd to anywhere, so why should it (what is it?) go "back to the original folder"? What exactly "isnt working"? Show the error messages.
And, give some data, like real filenames, as your sample "FilePrefix_20181120_075221_3.done" doesn't go well with the "${file:0:31}" shell expansion. Is your target directory meant to be an absolute path? Did you realize that your second expansion "${file##*.}" doesn't do anything as there's no . left in the file name?
On top of what my esteemed colleagues already said: you should not need to do any cd in any script! Use absolute pathes whenever possible (that effectively means: always), this will always guarantee that the script works the same from whereever you call it.
Thanks for the responses Madeingermany and Bakunin. I am using #!/bin/bash on the first line of the script. I am new to scripting, I will lookup and try the source script method for this. Hoping that works, will let you know if it does.
Bakunin, I had tried using absolute paths, as my statement has multiple steps in between, the value of 'file' is lost in each step.
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:
The first thing is: do NOT write one-liners. It is hard to understand what is going on when. So first, let us reformat:
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:
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:
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.
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)
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)
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)
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)
: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)