Linux recycle bin script - sh

I'm creating a recycle-bin script in SH shell linux in three differant scripts, delete, trash and restore.
The first two scripts are working fine; 'Delete' moves the selected file to the recycle-bin while logging a text file called 'trashinfo' which shows the original path location of the file (to be later used in restore) and 'Trash' which removes everything in the recycle-bin.
The 'restore' script should take the logged path name gained in the delete script and return the file to its original location. I've spent more time than I'd like to remember on this and cant get the restore script to work properly!
Below is the script I've written, as far as I can make out I'm grepping for the filename variable in the text file that holds the pathname, eg 'restore testfile', this is then combined with the basename command, the testfile is then moved into the location thats been grepped and combined with the basename.
Anyone have any pointers on where I'm going wrong?
if [ "$*" != -f ]
then
path=grep "$*" /usr/local/bin/trashinfo
pathname=basename "$path"
mv "$path" "$pathname"

path=$(grep "$*" /usr/local/bin/trashinfo)
pathname=$(basename "$path")

Related

How to get vim to list the PIDs of selected files that are presently being edited, avoiding recovery mode, and not list all the other files

The vim manual page contains two similar -r type commands. I'll give more background below, this question is really how to invoke the first type of -r to list the swap files, but avoid the second -r that invokes recovery
-r List swap files, with information about using them for re‐
covery.
-r {file} Recovery mode. The swap file is used to recover a crashed
editing session. The swap file is a file with the same
filename as the text file with ".swp" appended. See ":help
recovery".
The -r without filename (the first -r above ) reports on the swap files of other files too, including ones in other directories
Background:
I'm trying to have vim report the swap files of a specific file (mostly to determine if vim still editing the file). If the file is being edited ( in another window, either on linux or cygwin ) I can 'raise' that window up to the top with "\e[2t\e[1t" as I have successfully be able to do thanks to Bring Window to Front
Vim has multiple swap file names, and multiple directories that it could put a file, so I want to ask vim, please tell me the name of the swap files that are currently in use for a given file, and if there is a current vim process on the file. Unfortunately, sometimes vim will open a command file in recovery mode in unexpected ways.
I'm invoking vim like this vim -r -c :q file, well actually, I'm invoking it from script, since I want vim to see something more like a terminal, then I look at the output file, so it's more like script -q -c "vim -r -c :q foo" fooscript, then I look in the fooscript file for messages like /Note: process STILL RUNNING: (\d+)/
It is beginning to look like I need to use vim -r without a file name, and parse the output of the -r report, and that there isn't a way to get the report pre-filtered to a single file in question.
after switching my focus to just vim -r, and
Knowing that vim will try to put the swap file into the same directory as the file it's editing ( thanks to #romainl for the pointer to :help swap-file )
observing that vim -r reports on the files in the current directory first,
observing that the file name associated with the swap file is reported before the process id of the vim process, and
observing that vim appends (STILL RUNNING) if it finds the active process
I changed the current directory appropriately and ran this code after plugging in the name of the file-to-search-for
perl -lne '
last if /^\s+In directory/;
undef $f if /^\d+/;
$f = $1 if /^\s+file name:\s+(.*)\s*$/;
if ( $f =~ m#/file-to-search-for# && /^\s+ process ID:\s(\d+).*?STILL RUNNING/ ) {
print $1;
$pid //= $1;
}
END { exit !$pid; } '
The pid of the running vim process is printed, and the exit status is zero when the appropiate swap file is found, and non-zero if the file was not being edited

Rename and overwrite files using wildcard in Windows

I am working on a script for auto deployment, where I need to replace my files with the same filenames.
For example, I have the following files in my current directory
deployment.properties
wrapper.conf
config.properties
Later, I will generate another set of files like this
deployment.properties.tokenized
wrapper.conf.tokenized
config.properties.tokenized
Lastly, I want to replace the existing config files (in the first code block) using the *.tokenized version and remove the tokenized files.
In Linux, the following can do the job. But I don't know how to do in Windows
for f in *tokenized;
do mv "$f" "`echo $f | sed s/tokenized//`";
done
I tried to use powershell's move-item, rename-item but still cannot figure out the right way to do it. Could somebody help? bat / powershell scripts are both welcomed. Using loop is also okay. Thank you.
It is almost the same code but in cmd / batch files we have access to the elements of the file name.
From command line
for %a in (*.tokenized) do move /y "%a" "%~na"
Or, for a batch file (you need to escape the for replaceable parameter)
for %%a in (*.tokenized) do move /y "%%a" "%%~na"
As the extension of the file (the text after the last dot) is .tokenized, when you request just the name (without extension) of the file being referenced (%%~na) you get the original file name.
This PowerShell script should do the job:
Get-ChildItem *.tokenized | % {
move $_.Name ([System.IO.Path]::GetFileNameWithoutExtension($_.Name)) -Force
}

Mv file problems when moving file back in path

I am completing Zed Shaw's Learn Python the Hard Way and am stuck on a concept in the appendix command line crash course in Windows PowerShell.
My problem is with the move (mv) command, specifically moving a file farther back in the path (hope that makes sense). Here is what I did:
I created a directory called temp, and within that directory created a .txt file called awesome.txt and another dirctory called newplace. Then, I write the command "mv awesome.txt newplace" and the awesome.txt file is moved to the directory newplace. Great!
The problem is that I want to move the file awesome.txt back to its original place in the directory temp. When I change my working directory to the directory newplace "cd newplace" and then type "mv awesome.txt temp" the file awesome.txt does not move back to the directory temp, but instead converts from a .txt file to a "file" and stays put in the newplace directory.
Folders like this are nested inside each other:
c:\temp
c:\temp\newplace\
When you cd around, you go into a folder (, e.g. cd temp:
c:\temp\ (o_o)
c:\temp\newplace\
And you can only see things in the same folder you are in. So you can move into newplace because that name makes sense where you are. But when you are in newplace
c:\temp\
c:\temp\newplace\ (o_o)
You can't move to temp because you don't know where it is. You don't have an index of every directory name on the entire computer that you can shortcut to, you only have two options: something in the same place you are, or something with an absolute path - a full name of where it is. c:\temp\.
So mv awesome.txt temp tries to put it inside temp where you are -> c:\temp\newplace\temp\ -> that doesn't exist, so it assumes you're moving it to a new name in the same place.
You would need mv awesome.txt c:\temp\ to specify it properly.
Except there's a sneaky shortcut, anywhere you are, there is automagically a path called .. which means the folder one <-- thatway from where I am.
So you could mv awesome.txt ..\ to push it back one level in the path, without needing to know exactly where that is. This is probably what Zed Shaw is expecting.

Running a script in bash

I have a script in one of my application folders.Usually I just cd into that locatin in Unix box and run the script e.g.
UNIX> cd My\Folder\
My\Folder> MyScript
This prints the required result.
I am not sure how do I do this in Bash script.I have done the following
#!/bin/bash
mydir=My\Folder\
cd $mydir
echo $(pwd)
This basically puts me in the right folder to run the required script . But I am not sure how to run the script in the code?
If you can call MyScript (as opposed to ./MyScript), obviously the current directory (".") is part of your PATH. (Which, by the way, isn't a good idea.)
That means you can call MyScript in your script just like that:
#!/bin/bash
mydir=My/Folder/
cd $mydir
echo $(pwd)
MyScript
As I said, ./MyScript would be better (not as ambiguous). See Michael Wild's comment about directory separators.
Generally speaking, Bash considers everything that does not resolve to a builtin keyword (like if, while, do etc.) as a call to an executable or script (*) located somewhere in your PATH. It will check each directory in the PATH, in turn, for a so-named executable / script, and execute the first one it finds (which might or might not be the MyScript you are intending to run). That's why specifying that you mean the very MyScript in this directory (./) is the better choice.
(*): Unless, of course, there is a function of that name defined.
#!/bin/bash
mydir=My/Folder/
cd $mydir
echo $(pwd)
MyScript
I would rather put the name in quotes. This makes it easier to read and save against mistakes.
#!/bin/bash
mydir="My Folder"
cd "$mydir"
echo $(pwd)
./MyScript
Your nickname says it all ;-)
When a command is entered at the prompt that doesn't contain a /, Bash first checks whether it is a alias or a function. Then it checks whether it is a built-in command, and only then it starts searching on the PATH. This is a shell variable that contains a list of directories to search for commands. It appears that in your case . (i.e. the current directory) is in the PATH, which is generally considered to be a pretty bad idea.
If the command contains a /, no look-up in the PATH is performed. Instead an exact match is required. If starting with a / it is an absolute path, and the file must exist. Otherwise it is a relative path, and the file must exist relative to the current working directory.
So, you have two acceptable options:
Put your script in some directory that is on your PATH. Alternatively, add the directory containing the script to the PATH variable.
Use an absolute or relative path to invoke your script.

I want to prompt a user for the first 5 characters of a file then have it search for the files

I'm trying to write a script that will prompt the user for the first 5 charters of a file name then have it search the directories for any files that start with that. Then I want it to check to see if a folder is created with the file names and if not create one then move the files there. But if there is a directory for it already then to just move the files too the folder.
Break it down step by step:
"prompt the user for the first 5 characters of a file name" -- you can use the shell read command to get the data. Try a simple shell script:
#!/bin/bash
read foo
echo "foo = $foo"
"if a folder is created with the file names" -- you can use find to see if a file exists. for example:
find . -name abcde\*
"But if there is a directory for it already then to just move the files too the folder." -- the command mkdir takes a -p option so that, if the directory already exists, it won't do anything.