Trying to write a copy script that allows me to append the target file if it already exist - copy

So this program would allow me to copy files by entering the name of a source file then the name of a destination file. If the destination file already existed i want to give myself the choice to overwrite append or cancel. So far ive been able to get the choice part to work but i cant get the test if the file exist to work along side the choices. Here is what i have so far its not completed but any input on how to make this run would be much appreciated.
if [ $1 -f ]
then echo "file exist"
while [ $2 -e ]
read -p "Do you want to (O)verwrite, (A)ppend, or (E)nd? " answer
case ${answer:0:1} in
o|O )
cp $1 $2
;;
a|A )
cat $1 >> $2
;;
* )
echo "Cancel"
;;
esac
fi

I think its just a typo -
Change
if [ $1 -f ]
to
if [ -f $1 ]

Related

sh Script with copy and removing a part from filename

So i use a script that copy's specific files to specific folders based on there filenames. I would like to extend the script so that after the copy progress a part from the filename is removed. Here is an example The filename looks likes this Image_000058_19_12_2019_1920x1080.jpg and i like to remove the resolution (_1920x1080) part from it. Is there a way to add it to my Script (see below) Thanks to #fedxc for the script.
cd "$HOME/Downloads"
# for filename in *; do
find . -type f | while IFS= read filename; do # Look for files in all ~/Download sub-dirs
case "${filename,,*}" in # this syntax emits the value in lowercase: ${var,,*} (bash version 4)
*.part) : ;; # Excludes *.part files from being moved
move.sh) : ;;
# *test*) mv "$filename" "$HOME/TVshows/Glee/" ;; # Using move there is no need to {&& rm "$filename"}
*test*) scp "$filename" "imac#imac.local:/users/imac/Desktop/" && rm "$filename" ;;
*american*dad*) scp "$filename" "imac#imac.local:/users/imac/Movies/Series/American\ Dad/" && rm "$filename" ;;
*) echo "Don't know where to put $filename" ;;
esac
done```
I use variable operation for bash. Example:
export filename='Image_000058_19_12_2019_1920x1080.jpg' <----Setting name of filename
echo ${filename/_1920x1080/} <--Operation with bash variable.
Image_000058_19_12_2019.jpg <--Result of echo
Consult this page for more: Bash Guide

Bourne Shell Script

I'm attempting to write a script in the Bourne shell that will do the following:
Read in a filename
If the file does not exist in the target directory, it will display a message to the user stating such
If the file exists in the target directory, it will be moved to a /trash folder
If the file exists in the target directory, but a file of the same name is in the /trash folder, it will still move the file to the /trash directory, but will attach a _bak extention to the file.
My use of the Bourne shell is minimal, so here's what I have so far. Any pointers or tips would be greatly appreciated, thanks!
#!/bin/sh
#Scriptname: Trash Utility
source_dir=~/p6_tmp
target_dir=~/trash
echo "Please enter the filename you wish to trash:"
read filename
if [ -f $source_dir $filename]
then mv "$filename" "$target_dir"
else
echo "$filename does not exist"
fi
You cannot use ~ to refer to $HOME in a sh script. Switch to $HOME (or change the shebang to a shell which supports this, such as #!/bin/bash).
To refer to a file in a directory, join them with a slash:
if [ -f "$source_dir/$filename" ]
Notice also the required space before the terminating ] token.
To actually move the file you tested for, use the same expression for the source argument to mv:
mv "$source_dir/$filename" "$target_dir"
As a general design, a script which takes a command-line parameter is much easier to integrate into future scripts than one wich does interactive prompting. Most modern shells offer file name completion and history mechanisms, so a noninteractive script also tends to be more usable (you practically never need to transcribe a file name manually).
A Bash Solution:
#!/bin/bash
source_dir="~/p6_tmp"
target_dir="~/trash"
echo "Please enter the filename you wish to trash:"
read filename
if [ -f ${source_dir}/${filename} ]
then
if [ -f ${target_dir}/${filename} ]
then
mv "${source_dir}/${filename}" "${target_dir}/${filename}_bak"
else
mv "${source_dir}/${filename}" "$target_dir"
fi
else
echo "The file ${source_dir}/${filename} does not exist"
fi
Here's the completed script. Thanks again to all who helped!
#!/bin/sh
#Scriptname: Trash Utility
#Description: This script will allow the user to enter a filename they wish to send to the trash folder.
source_dir=~/p6_tmp
target_dir=~/trash
echo "Please enter the file you wish to trash:"
read filename
if [ -f "$source_dir/$filename" ]
then
if [ -f "$target_dir/$filename" ]
then mv "$source_dir/$filename" "$target_dir/$(basename "$filename")_bak"
date "+%Y-%m-%d %T - Trash renamed ~/$(basename "$source_dir")/$filename to ~/$(basename "/$target_dir")/$(basename "$filename")_bak" >> .trashlog
else mv "$source_dir/$filename" "$target_dir"
date "+%Y-%m-%d %T - Trash moved ~/$(basename "/$source_dir")/$filename to ~/$(basename "/$target_dir")/$filename" >> .trashlog
fi
else
date "+%Y-%m-%d %T - Trash of ~/$(basename "/$source_dir")/$filename does not exist" >> .trashlog
fi

Is there a way to launch emacs merge without first opening emacs and using M-x and more?

I sometimes want to merge multiple pairs of files, suppose I want to merge fileA.old and fileA.new, as well as fileB.old and fileB.new..and so on.Currently I have to open emacs. Do M-x ediff-merge-files and enter name of first file, return key, name of second file, return key..and im in merge mode...is there a way to launch emacs with both file names as arguments and land in merge mode?
You can pass Lisp code to Emacs through the command line:
emacs --eval '(ediff-merge-files "path/to/file1" "path/to/file2")'
Of course this could be wrapped in a script to make it more convenient to call. For instance, in a bourne shell, you could do a simple version like this:
#!/bin/sh
# check correct invocation
if [ $# != 2 ]; then
echo "USAGE: $(basename "${0}") <file1> <file2>"
exit 1
fi
# check that file1 exists and is readable
if [ -f "${1}" ]; then
if [ ! -r "${1}" ]; then
echo "Cannot open '${1}', access denied."
exit 3
fi
else
echo "File not found: '${1}'"
exit 2
fi
# check that file2 exists and is readable
if [ -f "${2}" ]; then
if [ ! -r "${2}" ]; then
echo "Cannot open '${2}', access denied."
exit 5
fi
else
echo "File not found: '${2}'"
exit 4
fi
# invoke emacs
emacs --eval "(ediff-merge-files \"${1}\" \"${2}\")"
If you save this in a file ediff on your $PATH, you can then simply write:
ediff file1 file2
on the command line and Emacs will pop up with the two given files in ediff-mode.

Convert Bash script to Perl/VBS/any other windows compatible language [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
I got a bash script that was made for transferring files from a Sandbox to a host.
The files in the sandboxdirectory get transferred only if they are younger, meaning they have been "touched" before. Unfortunately, cygwin is causing trouble under windows so I need the script in another language or I need something that works like cygwin under windows.
The script is just about 20 lines, but i don't have any clue how to convert it to another language(especially commands like touch, make, gcc, getopts, set -e)
I'd be happy if someone finds this easy to do and converts it:
# EXITING SCRIPT IF ERROR OCCURS
set -e
FORCE=false
JUSTPRINT=
# if parameter -f is given, force to transfer all files(no matter if new or not)
# if -n is given checking files
while getopts fn opt 2>/dev/null
do
case $opt in
f) FORCE=true ;;
n) JUSTPRINT="-n" ;;
?) ;;
esac
done
# deleting parsed options from command line
shift `expr $OPTIND - 1`
# refresh files that came with -f
if [ \( $FORCE = true \) -a \( $# -gt 0 \) ]
then
touch -c $#
fi
# Targets (dummy files for timestamp)
TARGETS=`for filename
do
if [ -f $filename ]
then
echo "../transport_dummies/\`basename $filename\`.dum"
else
echo "$filename"
fi
done`
# call script directory
echo $0
cd `dirname $0`
# creating sysfilterL.exe
if [ ! -f sysfilterL ]
then
echo "sysfilterL is created."
gcc sysfilter.c -o sysfilterL
fi
# Call Transport-Makefile with target
if [ $# -gt 0 ]
then
make --warn-undefined-variables $JUSTPRINT -f transportDE.mk reset $TARGETS
send_queueed
else
make --warn-undefined-variables $JUSTPRINT -f transportDE.mk
fi
You can convert the shell script to a batch file, which is covered here: How do I convert a bash shell script to a .bat file?
Also here is a list of corresponding commands in bash and batch: http://tldp.org/LDP/abs/html/dosbatch.html

pipe into conditional on command line

I have a problem i could not figure out if it's even possible. I am parsing a file with filenames in it, and want to check if those filenames represent an existing file within the system.
i figured out a possibility to to check if a file exists:
[ -f FILENAME ] && echo "File exists" || echo "File does not exists"
now my problem is: How can i pipe into to the conditional that it tests for all the filenames?
i was trying like tihs, but it did not work:
cat myfilenames.txt | xargs command from above without FILENAME
does anybody know if it is possible?
thanks, dmeu!
while read file; dp
[ -e "$file" ] && echo "$file exists";
done <filelist.txt
I believe what you want is a for loop. This worked for me in bash (I put it in a shell script, but you could probably do it on the command line):
for i in `cat $1` ; do
[ -f $i ] && echo File $i exists || echo File $i does not exist
done
the backticks around the cat execute the command and substitute the output into the loop.