Batch command to edit filestream - windows

'echo 0 > Q:\FactoryRecovery\RECOVERY.INI:Done'
Can anyone please explain to me how this command works? I'm curious after reading this Superuser post: https://superuser.com/questions/384658/why-can-i-only-create-one-factory-backup-from-my-lenovo-thinkpad
It allows you to create more than one recovery disk.

Ok, heres a brief expanation:
Echo 0
Simply outputs 0 to the screen. Thats not to complicated. adding a >> and a file path after this will redirect the output to the end of the file. Adding a > will replace the contents of the file with what is being redirected.
Hence:
Echo 0 > Q:\FactoryRecovery\RECOVERY.INI
Will replace the contents of Recovary.INI with 0(while it used to be 1).
It prevents the file needed to create additional recovary disks from exiting, essentially allowin you to create more than one.
Mona.

Related

Programming a "Not - Gate" situation into a batch file?

Brand new to coding of any form. Simply put: I have 2 batch files written. 1 loads one configuration into a program (MultiMonitor), the other loads a 2nd configuration. Id like a 3rd file to act as a "light switch" Or a "NOT-Gate." Effectively, if last time it was ran, it launched the bat controlling config 1, this time it should load config 2.
Currently I just launch the appropriate .bat; but id like to simplify this so I only have one file to launch and then it chooses the one not chosen last time.
This is easy:
In option1.bat (in addition to the real work):
echo call option2 > autoselect.bat
In option2.bat
echo call option1 > autoselect.bat
There are a few ways you could do this:
Use a permanent environmental variable using setx - NOT RECOMMENDED
Test which config is currently being used by your program - I am unfamiliar with your program so I do not know if this is even possible.
Use a temporary file
This example shows how to do this with a temp file:
if not exist file.ext (
rem load config #1
echo.>file.ext
) else (
rem load config #2
del file.ext
)
The file can be named whatever you want.

How to create a random unique file directly in /tmp using Ruby?

I am writing an application that creates and places a logfile in /tmp and afterwards moves this logfile to another directory. Unfortunately I faced some issues with this implementation and I would like to make this logfile more unique.
I came across mktemp, which would automatically create a file in /tmp. Perfect, just what I need! Unfortunately I cannot seem to get it to work in Ruby. I have tried the following without success:
def temporary_logfile
#temporary_logfile = `mktemp "#{File.basename($PROGRAM_NAME)}_#{Time.now.strftime('%Y%m%dT%H%M%S')}.logXXXX"`
end
I expected to see my logfile in /tmp but unfortunately nothing happens. I wonder what I did wrong?
The next step would be to use slice! to remove the random generated characters from mktemp from the logfile name and than move the file somewhere else.
Have a look at Tempfile: https://ruby-doc.org/stdlib-2.6.3/libdoc/tempfile/rdoc/Tempfile.html
file = Tempfile.new('foo')
begin
# ...do something with file...
ensure
file.close
file.unlink # deletes the temp file
end
Example is taken directly from the docu.

Merge lines in bash

I would like to write a script that restores a file, but preserving the changes that may be done after the backout file is created.
With more details: at some moment I create a backup of a file (file_orig). Do some changes to the original file as well(file_my_changes). After that, the original file can be changed again (file_additional_changes), but after the restore I want to have the backup file, plus the additional changes (file_orig + file_addtional_changes). In general backing out my changes only.
I am talking about grub.cfg file, so the expected possible changes will be adding or removing parts of a line.
Is it possible this to be done with a bash script?
I have 2 ideas:
Add some comments above the lines I am going to change, and then before the resotore if the line differ from the one from the backed out file, to read the comment, which will tell me what exactly to remove from the line;
If there is a way to display only the part of the line that differs from the file_orig and file_additional_changes, then to replace this line with the line from file_orig + the part that differs. But I am not sure if this is possible to be done at all.
Example"
line1: This is line1
line2: This is another line1
Is it possible to display only "another"?
Of course any other ideas are welcome!
Thank you!
Unclear, but perhaps if you're using a bash script you could run a diff on the 2 edited file and the last one and save that output someplace that you want to keep it? That would mean you have a copy of the changes.
Or just use git like everybody else.
One possibility would be to use POSIX commands patch and
diff.
Create the backup:
cp operational-file operational-file.001
Edit the operational file.
Create a patch from the differences:
diff -u operational-file.001 operational-file > operational-file.patch001
Copy the operational file again.
cp operational-file operational-file.002
Edit the operational file again.
Create a new patch
diff -u operational-file.002 operational-file > operational-file.patch002
If you need to recover but skip the changes from patch.001, then:
cp operational-file.001 operational-file
patch -i patch.002
This would apply just the second set of changes to the original file, as log as there's no overlap.
Consider using a version control system to keep records of the file changes. Consider using date/time stamps instead of version numbers on the file names.

Bash script , error output redirection

I want to redirect the error
python: can't open file '/usr/bin/file': [Errno 2] No such file or directory
I used this command
python /usr/bin/file 2> /dev/zero
but this is hiding all the output in my file , so I want to redirect only the not found error and nothing else .
Binary file is scripted by me through python giving this
Enter your password:
Welcome User :)
Checking your Secure Code now ...
Secure Code OK , Enjoy !!
What is the folder path ? ### This is hidden when used the code ###
You want /dev/null, not /dev/zero, which is a generator for scripts (you can read it for an endless stream of zero bytes if you ever need one).
The syntax you show does in fact redirect only standard error; if standard output is lost, it's because of something else.

What does this bash script function does

I am new to shell scripting and i found this function in a given script file.
##############################
# rotate_daily(filename)
rotate_daily() {
_code=0
_file_src=$1
_today=`date '+%Y-%m-%d'`
_file_dest=${_file_src}.${_today}
if [ -f ${_file_dest} ]; then
printk "rotate_daily(): ${_file_dest} already exist"
_code=1
else
if [ -f ${_file_src} ]; then
printk "rotate_daily(): ${_file_src} => ${_file_dest}"
cp -p ${_file_src} ${_file_dest}
_code=$?
>${_file_src}
fi
fi
}
I understand this is kind of coping file from one location to another location. But, it is not rotating right?. could somebody explain me what it is really does.
thanks in advance for any help
It copies _file_src to the location file_dest unless _file_dest already exists. An informative message will be printed that tells you if the file already exists or file_src_ will be copied, It also moves _file_src only if it is a file.
EDIT: forgot to mention what the command >{_file_src} does - it simply wipes out the contents of the source file. So you will have the contents of _file_src moved to file_dest in the end and _file_src will be empty. I can't figure why not simply do a move(with mv) and then create an empty file, but that's your question.
If the time stamped file already exists, this code snippet does nothing but print a message via printk indicating that. If it does not exist, it copies the source file to it and truncates the source file. I would guess that the line you are not quite understanding is:
>${_file_src}
That line truncates the original file after it has been copied. Note that there is a race condition, and any data written to the file between the copy and the truncation will be lost.

Resources