Facing issues while creating a shell script which satisfies conditions. - bash

Im trying to write a shell script which will help me to copy files from one directory to another satisfying some conditions.
It should first check if there is any directory with the name of the
file (yes, i said directory ) in the destination path. And if yes, then it should check whether,
the directory is having any files in it. If the condition is true, move
on to the next file. Else delete the directory and copy the file.
If the first condition is false (checking the file name with directory
name), copying should be done.
I succeed in truncating the extension (.TIF in this case) and storing the file names into a variable.
Currently i am facing an issue with comparing this variable with the folders in destination path.
Might be an easy case for majority of you guys, i am a newbie and iam
facing issues with the proper shell scripting.

Related

unix shell: how to escapte a hidden directory name "."

I am using AIX version 6.1.0.0. I have a hiden directory name like ".sh". When I place the directory name into a variable, I always get a cannot find file error.
file_dir=/opt/.sh/scripts
$file_dir/Find_files.sh $file_dir/file_name
Errors: cat: 0652-050 Cannot open "file_name". A file or directory in the path name does not exist.
I believe the issue is that hidden directory name ".sh". How can I go around this issue?
Without knowing the permissions on the file, directory, subdirs and which user your using its hard to be a 100% sure. But related information about this error states the following:
Possible Causes
1. This error message may be displayed during a backup operation.
2. The system looked for a file that either does not exist or was specified incorrectly.
3. The system looked for a file for which you do not have write permission.
Personally I think its a permission issue with your directory or subdir, so make sure you have the correct access or ownership.

Moving files in shell script and copying files to the same folder at the same time

I am writing a shell script to move all files from one directory to other. Say I want to move all files from directory dirA to directory dirB. When the move is in progress, some user is copying some files to dirA. How do I ensure that, whatever file the shell script is trying to move, has finished copying? What would happen if we try to move a file which is still being copied?
The list of filenames is generated as soon as the move command is issued. Any changes after that will not be taken into account. So if new files are created during the move process, they won't get impacted.
You may have to run your script in a loop.
If one of the files that is part of the move command is still being modified (after being opened in its old place), the changes would
propagate to the new place when the change operation is complete and the file is closed.

Move newly created text files to a var created directory

I have several text docs that are created each day from templates. This process I've achieved successfully albeit probably in a Cro-Magnon way. I want these newly created text files to be filed within a newly created dated folder.
The script creates the file docs from the templates successfully and also creates the newly dated directory. I don't really want to create these text files somewhere else and then move them to the newly created directory. Rather that they be created directly within it. All my research tends to involve directories that already exist rather than one created from a var.
I've included just one file creation example below.
Hope you can help. TIA
today=`date '+%y%m%d'`;
today_Folder=~/Desktop/test/"${today}"
if [[ ! -d $today_Folder ]]
then
mkdir "${today_Folder} `(date '+%A')`"
fi
cat ~/Desktop/test/template.txt >> ~/Desktop/test/dest.txt
P.S. I've tried to make the cat command regarding the text files clearer - it simply creates files. I'm NOT trying to create a tree of directories. Simply ONE newly created directory that could be in test along with the text files.
Your question is how to dynamically create a file, also creating all the path to contain that file? That's not possible in any intuitive/portable way, and it's not typically programs always have to create the directory before the file. What you can do is pass the -p flag to mkdir. On Linux systems (this may also not be portable), this flag means "create all the directories necessary for this path". Zero directories is okay, so you don't need to check whether the directory already exists. So change the whole if block to just this:
mkdir -p "${today_Folder} `(date '+%A')`"
Also, it's kind of smelly the way you want a string (the path) and you're using three operations to create it. Could it be simpler? You want more statements when they add clarity, but in this case the steps are so simple that the only thing accomplished is to make your colleagues go up and read what you wrote more than once. It might suit to change it to:
dir_path=...
mkdir -p "${dir_path}"
To accomplish this, keep in mind that instead of backticks, you can add command substitution with $(). It helps since backticks can't be nested--it makes the line more readable, since you clearly see the command's start/end.

CLIPS saving files to Virtualstore despite adding path

I am trying to save rules and facts to a specific directory in my CLIPS programs.
(save "c:\tmp\rules.clp")
(save-facts "c:\tmp\facts1")
1- But it seems CLIPS disregards the path information. Since my windows username is not the owner of the CLIPS installation directory, the files are saved in a virtual store directory:
C:\Users\USERNAME\AppData\Local\VirtualStore\Program Files (x86)\CLIPS\Bin
2- If I run a system command and include the path, the same thing happens. The path is disregarded and the notepad cannot find the intended file.
(system "notepad c:\tmp\output.txt")
Is there a way to force these commands to save/read from a specific directory?
CLIPS doesn't have the ability to override directory/files permissions set by the operating system. The path to the save/save-facts command is not modified before being passed to the system libraries for opening files (either fopen or fopen_s). I'd suggest changing the directory permissions if the user account you're using doesn't have write privileges.
I think I have found the problem.
The paths should both be included in double quotes and the slashes should be converted into forward slashes.

Comparing variable with File names bash

I've recently started to learn bash script and have started to create a file repository system. I have gotten pretty far and am able to add files, remove files. When I remove a file from the repository it actually leaves the file in but changes permissions so only the user that removed can use it, it then send a copy to there home area, it also changes the name of the file left behind to "$fileNameOUT"
I know plan to add a feature to my add function which checks after a file has been added if there is a file with the same name but with "OUT" at the end, if it finds this the old file will be sent to a back folder so files can be restored. I know I have to loop through the directory using a for loop, however the problem I'm having is I don't know how I can compare the file I have just added to all of the files in the directory.
I hope someone can make send of what I just wrote.
If you know the name of the file you are interested in, you can use the -e test to check if it exists.
if [ -e fooOUT ]
then
echo File exists
fi

Resources