Rename_Screenshots_For_String.sh "Button Flashing"
Hi Guysfiles, filtered by a string given by the user, from 01 to the length of
I'm working with Cygwin under Windows 10 I have written a program in
AWK / Sh to copy or rename some selected
Before you say that this task can be done with another software thanAWK, why I used this, OK, I know a bit only AWK and Bash scripting, and
But the thing is after so many hours struggling with this stuff, Iwant to know, how can I get these two variables (old and new filenames)
When I see a video tutorial, I take screenshots sporadically whenactions are done.
some
The thing is that the screenshots have generic names derived fromdate and the name of the video and therefore can be very long, as you
the
----------------------------------------------------script with an argument, which is a part of the screenshot name. My AWK
2021-09-08 10_10_42-PyQt5 - Apply your own Style using CSS - second method - updated - YouTube.png
2021-09-08 10_10_55-PyQt5 - Apply your own Style using CSS - second method - updated - YouTube.png
2021-09-08 10_13_37-Python PyQt5 -5- Make your first interaction with buttons using Signals Slots - .png
2021-09-08 10_13_46-Python PyQt5 -5- Make your first interaction with buttons using Signals Slots - .png
2021-09-10 07_08_53-Make Button Flashing _ PyQt5 Tutorial - YouTube.png 2021-09-10 07_12_40-Make Button Flashing _ PyQt5 Tutorial - YouTube.png 2021-09-15 09_19_39-Window.Window.Window.Window.png
2021-09-15 09_19_54-Window.png
2021-09-15 09_20_06-Window.png ----------------------------------------------------
Due to his issue, I wanted to give the user the possibility, to run
my
For example:
2021-09-10 07_08_53-Make Button Flashing _ PyQt5 Tutorial - YouTube.png 2021-09-10 07_12_40-Make Button Flashing _ PyQt5 Tutorial - YouTube.png
with the given string as: " Button Flashing" is changed to
01_Button Flashing.png
02_Button Flashing.png
Now my problem:
I have managed to get the string from the user and sort the files in the directory by time and this string, and to get the old_file and new_file names.
I'm struggling since a couple of days to copy or move these files from old names to new names, vain and futile. It brings, all the time, that something is wrong with the destination and I have to check with "cp –help".
I think one problem is due to the fact that I'm using Linux commands (Cygwin) under Windows (CR/LF problem).
Another problem is that the filenames have spaces, and therefore I had to replace them forwards and backwards with some Characters. And the error messages after running the script has to do with this issue.
Because I found after a couple of hours, the given string had one character more than I need (I think CR/LF). Therefore, I had to get rid of the last character, as you can see in my code.
I have tried all combinations with system() command or print, no success. I hope someone can help me with this. I don't see the jungle because of all the trees.
Any help would be very appreciated.
Here is the output of the script: &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
Rename_Screenshots_For_String.sh "Button Flashing"
================================================
Date and given arguments
================================================
Generated on: 2021_09_21.05_05
TOP = /cygdrive/c/Users/m.owzar/Desktop/teesst
String = Button Flashing
DATUM = 2021_09_21.05_05
================================================
*** 2021-09-10 07_08_53-Make Button Flashing _ PyQt5 Tutorial - YouTube.png --> 01_Button_Flashing.png
*** 2021-09-10 07_12_40-Make Button Flashing _ PyQt5 Tutorial - YouTube.png --> 02_Button_Flashing.png
2 Files were found!
--------------------------------------------
71 : 2021-09-10 07_08_53-Make Button Flashing _ PyQt5 Tutorial - YouTube.png 22 : 01_Button_Flashing.png
/bin/cp: zusätzlicher Operand 'Button'
„/bin/cp --help“ liefert weitere Informationen.
71 : 2021-09-10 07_12_40-Make Button Flashing _ PyQt5 Tutorial - YouTube.png 22 : 02_Button_Flashing.png
/bin/cp: zusätzlicher Operand 'Button'
„/bin/cp --help“ liefert weitere Informationen. &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
And here is my code:
#! /usr/bin/bash
# Filename: Rename_Screenshots_For_String.sh #########################################################
String=$1
DATUM=`date +"%Y_%m_%d.%H_%M"`
echo ""
echo "================================================"
echo "Date and given arguments"
echo "================================================"
echo "Generated on: " $DATUM
# =======================================================
ls -rt | grep -i "$1" | awk -v TOP="$(pwd)" -v String="$String" -v DATUM="$DATUM" '
########################################
## Begin Part
########################################
BEGIN {
print "TOP = " TOP
print "String = " String
print "DATUM = " DATUM
print "================================================"
}
########################################
## Main Part
########################################
{
if ($0 ~ / /) {
A = gensub(/ /, "_XXX_", "g", $0)
Arr_A[++N] = A
}
B = gensub(/_XXX_/, " ", "g", A)
Arr_B[++M] = B
}
########################################
## End Part
########################################
END {
Str = gensub(/ /, "_", "g", String)
for (i = 1; i <= M; i++) {
old_file[i] = Arr_B[i]
K = split(old_file[i], ARRAY, ".")
Extension = ARRAY[K]
Str_2 = substr(Str, 1, length(Str)-1)
new_file[i] = sprintf("%02d_%s.%s", i, Str_2, Extension)
printf "*** %s --> %s\n", old_file[i], new_file[i]
}
printf "\n%s Files were found!\n", M
print "--------------------------------------------"
for (i = 1; i <= M; i++) {
print length(old_file[i]), ": ", old_file[i]
print length(new_file[i]), ": ", new_file[i]
system("/bin/cp -T " old_file[i] " " new_file[i])
# print ""old_file[i]"" > new_file[i]
}
}
'
A couple comments, pick your options...
Generally it's simpler to do the file manipulation in shell.
(If you provide a terse example and clear description the folks
(here or better in comp.unix.shell) will help you.)
If possible, get rid of punctuation characters and spaces.
Specifically get rid of the quotes as part of the file names.
Keep punctuation in date/time and gsub() all other punctuation
to become spaces. Then gsub() all sequences of spaces to single
underscore (for example).
Since the source file name still has spaces you need to quote the
whole file name that you provide as argument to mv/cp as in
filename = "\"" filename "\""
To do the renaming either construct the shell commands and print
them for execution awk '{printf ... }' | sh or use system(...)
to do it
cmd = "mv -i \"" filename "\" " newname
To prevent name clashes of the transformed names memorize them in
an array and (for example) add numbering if necessary
if (newname in names) names[newname]++
newname = names[newname] newname
For the shell 'mv' command use shell option '-i' to create an
error instead of overwriting existing files (just in case that
even the numbering creates an already existing file name).
There's probably even more hints necessary. A concise post with
a clear example and concrete questions will help you to get your
answers.
HTH.
Janis
PS: And try to keep your line lengths in your posts <78 characters;
this is still Usenet (not a webforum), search for Usenet Netiquette.
On 21.09.21 05:33, Mohsen Owzar wrote:
Hi Guysfiles, filtered by a string given by the user, from 01 to the length of
I'm working with Cygwin under Windows 10 I have written a program in
AWK / Sh to copy or rename some selected
the selected files by the filter.
Before you say that this task can be done with another software thanAWK, why I used this, OK, I know a bit only AWK and Bash scripting, and
if you know another and more effective method than this one, do not
hesitate and offer your suggestion.
But the thing is after so many hours struggling with this stuff, Iwant to know, how can I get these two variables (old and new filenames)
out of AWK to copy or move
When I see a video tutorial, I take screenshots sporadically whenactions are done.
some
The thing is that the screenshots have generic names derived fromdate and the name of the video and therefore can be very long, as you
the
can see in the list below:
----------------------------------------------------script with an argument, which is a part of the screenshot name. My AWK program picks this string and takes files containing this string and
2021-09-08 10_10_42-PyQt5 - Apply your own Style using CSS - second method - updated - YouTube.png
2021-09-08 10_10_55-PyQt5 - Apply your own Style using CSS - second method - updated - YouTube.png
2021-09-08 10_13_37-Python PyQt5 -5- Make your first interaction with buttons using Signals Slots - .png
2021-09-08 10_13_46-Python PyQt5 -5- Make your first interaction with buttons using Signals Slots - .png
2021-09-10 07_08_53-Make Button Flashing _ PyQt5 Tutorial - YouTube.png 2021-09-10 07_12_40-Make Button Flashing _ PyQt5 Tutorial - YouTube.png 2021-09-15 09_19_39-Window.Window.Window.Window.png
2021-09-15 09_19_54-Window.png
2021-09-15 09_20_06-Window.png ----------------------------------------------------
Due to his issue, I wanted to give the user the possibility, to run
my
change their names to names beginning with two digits for 01 to the
length of the selected files and concatenated with the given string.
For example:
2021-09-10 07_08_53-Make Button Flashing _ PyQt5 Tutorial - YouTube.png 2021-09-10 07_12_40-Make Button Flashing _ PyQt5 Tutorial - YouTube.png with the given string as: " Button Flashing" is changed to
01_Button Flashing.png
02_Button Flashing.png
Now my problem:
I have managed to get the string from the user and sort the files in the directory by time and this string, and to get the old_file and new_file names.
I'm struggling since a couple of days to copy or move these files from old names to new names, vain and futile. It brings, all the time, that something is wrong with the destination and I have to check with "cp –help".
I think one problem is due to the fact that I'm using Linux commands (Cygwin) under Windows (CR/LF problem).
Another problem is that the filenames have spaces, and therefore I had to replace them forwards and backwards with some Characters. And the error messages after running the script has to do with this issue.
Because I found after a couple of hours, the given string had one character more than I need (I think CR/LF). Therefore, I had to get rid of the last character, as you can see in my code.
I have tried all combinations with system() command or print, no success. I hope someone can help me with this. I don't see the jungle because of all the trees.
Any help would be very appreciated.
Here is the output of the script: &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
Rename_Screenshots_For_String.sh "Button Flashing"
================================================
Date and given arguments
================================================
Generated on: 2021_09_21.05_05
TOP = /cygdrive/c/Users/m.owzar/Desktop/teesst
String = Button Flashing
DATUM = 2021_09_21.05_05
================================================
*** 2021-09-10 07_08_53-Make Button Flashing _ PyQt5 Tutorial - YouTube.png --> 01_Button_Flashing.png
*** 2021-09-10 07_12_40-Make Button Flashing _ PyQt5 Tutorial - YouTube.png --> 02_Button_Flashing.png
2 Files were found!
--------------------------------------------
71 : 2021-09-10 07_08_53-Make Button Flashing _ PyQt5 Tutorial - YouTube.png
22 : 01_Button_Flashing.png
/bin/cp: zusätzlicher Operand 'Button'
„/bin/cp --help“ liefert weitere Informationen.
71 : 2021-09-10 07_12_40-Make Button Flashing _ PyQt5 Tutorial - YouTube.png
22 : 02_Button_Flashing.png
/bin/cp: zusätzlicher Operand 'Button'
„/bin/cp --help“ liefert weitere Informationen. &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
And here is my code:
#! /usr/bin/bash
# Filename: Rename_Screenshots_For_String.sh #########################################################
String=$1
DATUM=`date +"%Y_%m_%d.%H_%M"`
echo ""
echo "================================================"
echo "Date and given arguments"
echo "================================================"
echo "Generated on: " $DATUM
# =======================================================
ls -rt | grep -i "$1" | awk -v TOP="$(pwd)" -v String="$String" -v DATUM="$DATUM" '
########################################
## Begin Part
########################################
BEGIN {
print "TOP = " TOP
print "String = " String
print "DATUM = " DATUM
print "================================================"
}
########################################
## Main Part
########################################
{
if ($0 ~ / /) {
A = gensub(/ /, "_XXX_", "g", $0)
Arr_A[++N] = A
}
B = gensub(/_XXX_/, " ", "g", A)
Arr_B[++M] = B
}
########################################
## End Part
########################################
END {
Str = gensub(/ /, "_", "g", String)
for (i = 1; i <= M; i++) {
old_file[i] = Arr_B[i]
K = split(old_file[i], ARRAY, ".")
Extension = ARRAY[K]
Str_2 = substr(Str, 1, length(Str)-1)
new_file[i] = sprintf("%02d_%s.%s", i, Str_2, Extension)
printf "*** %s --> %s\n", old_file[i], new_file[i]
}
printf "\n%s Files were found!\n", M
print "--------------------------------------------"
for (i = 1; i <= M; i++) {
print length(old_file[i]), ": ", old_file[i]
print length(new_file[i]), ": ", new_file[i]
system("/bin/cp -T " old_file[i] " " new_file[i])
# print ""old_file[i]"" > new_file[i]
}
}
'
A couple comments, pick your options...
Generally it's simpler to do the file manipulation in shell.
(If you provide a terse example and clear description the folks
(here or better in comp.unix.shell) will help you.)
If possible, get rid of punctuation characters and spaces.
Specifically get rid of the quotes as part of the file names.
Keep punctuation in date/time and gsub() all other punctuation
to become spaces. Then gsub() all sequences of spaces to single
underscore (for example).
Since the source file name still has spaces you need to quote the
whole file name that you provide as argument to mv/cp as in
filename = "\"" filename "\""
To do the renaming either construct the shell commands and print
them for execution awk '{printf ... }' | sh or use system(...)
to do it
cmd = "mv -i \"" filename "\" " newname
To prevent name clashes of the transformed names memorize them in
an array and (for example) add numbering if necessary
if (newname in names) names[newname]++
newname = names[newname] newname
For the shell 'mv' command use shell option '-i' to create an
error instead of overwriting existing files (just in case that
even the numbering creates an already existing file name).
There's probably even more hints necessary. A concise post with
a clear example and concrete questions will help you to get your
answers.
HTH.
Janis
PS: And try to keep your line lengths in your posts <78 characters;
this is still Usenet (not a webforum), search for Usenet Netiquette.
On 21.09.21 05:33, Mohsen Owzar wrote:
Hi Guysfiles, filtered by a string given by the user, from 01 to the length of
I'm working with Cygwin under Windows 10 I have written a program in
AWK / Sh to copy or rename some selected
the selected files by the filter.
Before you say that this task can be done with another software thanAWK, why I used this, OK, I know a bit only AWK and Bash scripting, and
if you know another and more effective method than this one, do not
hesitate and offer your suggestion.
But the thing is after so many hours struggling with this stuff, Iwant to know, how can I get these two variables (old and new filenames)
out of AWK to copy or move
When I see a video tutorial, I take screenshots sporadically whenactions are done.
some
The thing is that the screenshots have generic names derived fromdate and the name of the video and therefore can be very long, as you
the
can see in the list below:
----------------------------------------------------script with an argument, which is a part of the screenshot name. My AWK program picks this string and takes files containing this string and
2021-09-08 10_10_42-PyQt5 - Apply your own Style using CSS - second method - updated - YouTube.png
2021-09-08 10_10_55-PyQt5 - Apply your own Style using CSS - second method - updated - YouTube.png
2021-09-08 10_13_37-Python PyQt5 -5- Make your first interaction with buttons using Signals Slots - .png
2021-09-08 10_13_46-Python PyQt5 -5- Make your first interaction with buttons using Signals Slots - .png
2021-09-10 07_08_53-Make Button Flashing _ PyQt5 Tutorial - YouTube.png 2021-09-10 07_12_40-Make Button Flashing _ PyQt5 Tutorial - YouTube.png 2021-09-15 09_19_39-Window.Window.Window.Window.png
2021-09-15 09_19_54-Window.png
2021-09-15 09_20_06-Window.png ----------------------------------------------------
Due to his issue, I wanted to give the user the possibility, to run
my
change their names to names beginning with two digits for 01 to the
length of the selected files and concatenated with the given string.
For example:
2021-09-10 07_08_53-Make Button Flashing _ PyQt5 Tutorial - YouTube.png 2021-09-10 07_12_40-Make Button Flashing _ PyQt5 Tutorial - YouTube.png with the given string as: " Button Flashing" is changed to
01_Button Flashing.png
02_Button Flashing.png
Now my problem:
I have managed to get the string from the user and sort the files in the directory by time and this string, and to get the old_file and new_file names.
I'm struggling since a couple of days to copy or move these files from old names to new names, vain and futile. It brings, all the time, that something is wrong with the destination and I have to check with "cp –help".
I think one problem is due to the fact that I'm using Linux commands (Cygwin) under Windows (CR/LF problem).
Another problem is that the filenames have spaces, and therefore I had to replace them forwards and backwards with some Characters. And the error messages after running the script has to do with this issue.
Because I found after a couple of hours, the given string had one character more than I need (I think CR/LF). Therefore, I had to get rid of the last character, as you can see in my code.
I have tried all combinations with system() command or print, no success. I hope someone can help me with this. I don't see the jungle because of all the trees.
Any help would be very appreciated.
Here is the output of the script: &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
Rename_Screenshots_For_String.sh "Button Flashing"
================================================
Date and given arguments
================================================
Generated on: 2021_09_21.05_05
TOP = /cygdrive/c/Users/m.owzar/Desktop/teesst
String = Button Flashing
DATUM = 2021_09_21.05_05
================================================
*** 2021-09-10 07_08_53-Make Button Flashing _ PyQt5 Tutorial - YouTube.png --> 01_Button_Flashing.png
*** 2021-09-10 07_12_40-Make Button Flashing _ PyQt5 Tutorial - YouTube.png --> 02_Button_Flashing.png
2 Files were found!
--------------------------------------------
71 : 2021-09-10 07_08_53-Make Button Flashing _ PyQt5 Tutorial - YouTube.png
22 : 01_Button_Flashing.png
/bin/cp: zusätzlicher Operand 'Button'
„/bin/cp --help“ liefert weitere Informationen.
71 : 2021-09-10 07_12_40-Make Button Flashing _ PyQt5 Tutorial - YouTube.png
22 : 02_Button_Flashing.png
/bin/cp: zusätzlicher Operand 'Button'
„/bin/cp --help“ liefert weitere Informationen. &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
And here is my code:
#! /usr/bin/bash
# Filename: Rename_Screenshots_For_String.sh #########################################################
String=$1
DATUM=`date +"%Y_%m_%d.%H_%M"`
echo ""
echo "================================================"
echo "Date and given arguments"
echo "================================================"
echo "Generated on: " $DATUM
# =======================================================
ls -rt | grep -i "$1" | awk -v TOP="$(pwd)" -v String="$String" -v DATUM="$DATUM" '
########################################
## Begin Part
########################################
BEGIN {
print "TOP = " TOP
print "String = " String
print "DATUM = " DATUM
print "================================================"
}
########################################
## Main Part
########################################
{
if ($0 ~ / /) {
A = gensub(/ /, "_XXX_", "g", $0)
Arr_A[++N] = A
}
B = gensub(/_XXX_/, " ", "g", A)
Arr_B[++M] = B
}
########################################
## End Part
########################################
END {
Str = gensub(/ /, "_", "g", String)
for (i = 1; i <= M; i++) {
old_file[i] = Arr_B[i]
K = split(old_file[i], ARRAY, ".")
Extension = ARRAY[K]
Str_2 = substr(Str, 1, length(Str)-1)
new_file[i] = sprintf("%02d_%s.%s", i, Str_2, Extension)
printf "*** %s --> %s\n", old_file[i], new_file[i]
}
printf "\n%s Files were found!\n", M
print "--------------------------------------------"
for (i = 1; i <= M; i++) {
print length(old_file[i]), ": ", old_file[i]
print length(new_file[i]), ": ", new_file[i]
system("/bin/cp -T " old_file[i] " " new_file[i])
# print ""old_file[i]"" > new_file[i]
}
}
'
[...]
Other question in this regard
If I want to ask user to copy, rename or abort (Cc, Rr or Nn)
before invoking the system() command
I used the following snippet code
::::::::::::::::::::::::::::::::::::::::::
for (i = 1; i <= M; i++) {
print length(old_file[i]), ": ", old_file[i]
print length(new_file[i]), ": ", new_file[i]
printf "Do you want to Copy [Cc] or Rename [Rr], otherwise [Nn]:"
getline answer < "-"
answer = tolower(answer)
if (answer == "c")
cmd = "cp -i \"" old_file[i] "\" " new_file[i]
else if (answer == "r")
cmd = "mv -i \"" old_file[i] "\" " new_file[i]
else
print "You ommited Copying / Renaming files!"
system(cmd)
}
::::::::::::::::::::::::::::::::::::::::::
It doesn't stop at the line containing the "getline" command and waits for user answer.
It goes through all if, else if and else part and gives out
only the message from the else part, e.g.
Do you want to Copy [Cc] or Rename [Rr], otherwise [Nn]:You ommited Copying / Renaming files!
can you also help me out of this dilema?
Hi Guys<snip>
I'm working with Cygwin under Windows 10
I have written a program in AWK / Sh to copy or rename some selected files, filtered by a string given by the user, from 01 to the length of the selected files by the filter.
On 21.09.21 13:16, Mohsen Owzar wrote:
[...]
Other question in this regard
If I want to ask user to copy, rename or abort (Cc, Rr or Nn)
before invoking the system() command
I used the following snippet code
::::::::::::::::::::::::::::::::::::::::::
for (i = 1; i <= M; i++) {
print length(old_file[i]), ": ", old_file[i]
print length(new_file[i]), ": ", new_file[i]
printf "Do you want to Copy [Cc] or Rename [Rr], otherwise [Nn]:"
getline answer < "-"
answer = tolower(answer)
if (answer == "c")
cmd = "cp -i \"" old_file[i] "\" " new_file[i]
else if (answer == "r")
cmd = "mv -i \"" old_file[i] "\" " new_file[i]
else
print "You ommited Copying / Renaming files!"
system(cmd)
}
::::::::::::::::::::::::::::::::::::::::::
It doesn't stop at the line containing the "getline" command and waits for user answer.
It goes through all if, else if and else part and gives out
only the message from the else part, e.g.
Do you want to Copy [Cc] or Rename [Rr], otherwise [Nn]:You ommited Copying / Renaming files!
can you also help me out of this dilema?
Probably not, because there's not enough information for me to
judge, and 'getline' is a problematic statement anyway. There's
several uncertainties I'd have. - Is your code above part of the
regular standard-input processing loop or executed in the BEGIN
clause? Is "-" a valid standard representation for the standard
input channel in your version of awk? What awk are you using?
There's also no >0 test coded whether getline succedded; it may
provide useful diagnostics. Maybe getline answer < "/dev/tty"
works better? How is variable 'cmd' initialized? What are the
requirements when nothing is entered; executing an empty string,
or the 'cmd' variable's value from the previously looped file?
Janis
After inserting "/dev/tty" instead of "-", it waits for the user input
and it copies when I type "c" and Enter.
In other words, it means that I can have user inputs from within AWK
only with:
getline answer < "/dev/tty"
Other than this method, there is no other possibility to get user
input, not in the BEGIN part for the command arguments?
mvDo mv on aaa
cpDo cp on bbb
On 21.09.21 15:03, Janis Papanagnou wrote:
On 21.09.21 13:16, Mohsen Owzar wrote:
[...]
Other question in this regard
If I want to ask user to copy, rename or abort (Cc, Rr or Nn)
before invoking the system() command
I used the following snippet code
::::::::::::::::::::::::::::::::::::::::::
for (i = 1; i <= M; i++) {
print length(old_file[i]), ": ", old_file[i]
print length(new_file[i]), ": ", new_file[i]
printf "Do you want to Copy [Cc] or Rename [Rr], otherwise [Nn]:"
getline answer < "-"
answer = tolower(answer)
if (answer == "c")
cmd = "cp -i \"" old_file[i] "\" " new_file[i]
else if (answer == "r")
cmd = "mv -i \"" old_file[i] "\" " new_file[i]
else
print "You ommited Copying / Renaming files!"
system(cmd)
}
::::::::::::::::::::::::::::::::::::::::::
It doesn't stop at the line containing the "getline" command and waits for user answer.
It goes through all if, else if and else part and gives out
only the message from the else part, e.g.
Do you want to Copy [Cc] or Rename [Rr], otherwise [Nn]:You ommited Copying / Renaming files!
can you also help me out of this dilema?
Probably not, because there's not enough information for me toReinspecting code from your original post it seems you are feeding
judge, and 'getline' is a problematic statement anyway. There's
several uncertainties I'd have. - Is your code above part of the
regular standard-input processing loop or executed in the BEGIN
clause? Is "-" a valid standard representation for the standard
input channel in your version of awk? What awk are you using?
There's also no >0 test coded whether getline succedded; it may
provide useful diagnostics. Maybe getline answer < "/dev/tty"
works better? How is variable 'cmd' initialized? What are the
requirements when nothing is entered; executing an empty string,
or the 'cmd' variable's value from the previously looped file?
filenames through the standard input channel (i.e. using pipes),
so if you also use the same channel for user's commands selection
input you will have a problem. Using, as proposed, "/dev/tty"
instead of "- might thus already solve the issue with your input
channels, but I haven't analyzed your code so that there might be
yet more issues (like the also mentioned uninitialized variables,
etc.).
Janis
gawk --versionGNU Awk 5.1.0, API: 3.0 (GNU MPFR 4.0.2-p6, GNU MP 6.2.0)
gawk ' {printf "Type a number: "; getline a < "-"; print "You typed " a} '
gawk ' {printf "number?: "; getline a < "/dev/tty"; print "a = " a} '
On 22.09.21 12:29, Mohsen Owzar wrote:
After inserting "/dev/tty" instead of "-", it waits for the user input
and it copies when I type "c" and Enter.
In other words, it means that I can have user inputs from within AWK
only with:
getline answer < "/dev/tty"
Other than this method, there is no other possibility to get userWell, you can get user-input from standard input as well, but
input, not in the BEGIN part for the command arguments?
then you can't also read the filenames from that channel (as
you seem to do in your code).
Standard input can always be redirected (to pipe, files, etc.).
With /dev/tty you are directly accessing the device; thus this
appears to be the preferred way to get interactive user input.
As I understand your post, it works with that approach. But
your question seems to say that you're uncomfortable with it?
You could of course also use other channels, e.g. passing the
file names as arguments, as in
$ awk 'BEGIN { for (i = 1; i <= ARGC; i++) {
printf "%s> ", ARGV[i]
getline cmd < "-"
printf "Do %s on %s\n", cmd, ARGV[i]
}
}' aaa bbb
mvDo mv on aaa
cpDo cp on bbb
Janis
On 21/09/2021 04:33, Mohsen Owzar wrote:
Hi Guys
I'm working with Cygwin under Windows 10
I have written a program in AWK / Sh to copy or rename some selected
files, filtered by a string given by the user, from 01 to the length
of the selected files by the filter.
Before you say that this task can be done with another software than
AWK, why I used this, OK, I know a bit only AWK and Bash scripting,
and if you know another and more effective method than this one, do
not hesitate and offer your suggestion.
Something like this in bash:
N=1
for X in "*$1*.png" ; do
echo cp "$X" "$(printf '%02d_%s.png' N "$1" )"
N = $((N+1))
done
Remove the 'echo' when you've finished testing!
Hi Guys
I'm working with Cygwin under Windows 10
I have written a program in AWK / Sh to copy or rename some selected files, filtered by a string given by the user, from 01 to the length of the selected files by the filter.
Before you say that this task can be done with another software than AWK, why I used this, OK, I know a bit only AWK and Bash scripting, and if you know another and more effective method than this one, do not hesitate and offer your suggestion.
On 23/09/2021 00:06, Janis Papanagnou wrote:
Something like this in bash:
N=1
for X in "*$1*.png" ; do
Isn't it *"$1"*.png
Don't know for sure - I think they're both the same or the same under
most conditions, at least.
On 23.09.21 00:02, Bruce Horrocks wrote:
On 21/09/2021 04:33, Mohsen Owzar wrote:
Hi Guys
I'm working with Cygwin under Windows 10
I have written a program in AWK / Sh to copy or rename some selected
files, filtered by a string given by the user, from 01 to the length
of the selected files by the filter.
Before you say that this task can be done with another software than
AWK, why I used this, OK, I know a bit only AWK and Bash scripting,
and if you know another and more effective method than this one, do
not hesitate and offer your suggestion.
Something like this in bash:
N=1
for X in "*$1*.png" ; do
Isn't it *"$1"*.png
echo cp "$X" "$(printf '%02d_%s.png' N "$1" )"
N = $((N+1))
and N=$((N+1)) ?
done
Remove the 'echo' when you've finished testing!
Isn't it *"$1"*.png
Don't know for sure - I think they're both the same or the same under
most conditions, at least.
echo cp "$X" "$(printf '%02d_%s.png' N "$1" )"
N = $((N+1))
and N=$((N+1)) ?
Yes, no spaces is correct - I so rarely code in bash I forget and want
to pretty-print it!
In article <5c9ea403-262b-f93e-532a-7fc2453afb87@scorecrow.com>,
Bruce Horrocks <07.013@scorecrow.com> wrote:
...
Isn't it *"$1"*.png
Don't know for sure - I think they're both the same or the same under
most conditions, at least.
I just did a little testing, to confirm my original thoughts.
Generally, no, it's not the same. Double quoting inhibits filename expandsion, but it works as expected in a "for i in ..." loop, because it gets scanned twice in that one, specific case.
echo cp "$X" "$(printf '%02d_%s.png' N "$1" )"
N = $((N+1))
and N=$((N+1)) ?
Yes, no spaces is correct - I so rarely code in bash I forget and want
to pretty-print it!
Or you could do:
: $((N++))
and there's probably other ways, as well.
Or just: echo cp "$X" "$(printf '%02d_%s.png' $((N++)) "$1" )"
On 23/09/2021 00:06, Janis Papanagnou wrote:
On 23.09.21 00:02, Bruce Horrocks wrote:
On 21/09/2021 04:33, Mohsen Owzar wrote:
Hi Guys
I'm working with Cygwin under Windows 10
I have written a program in AWK / Sh to copy or rename some selected
files, filtered by a string given by the user, from 01 to the length
of the selected files by the filter.
Before you say that this task can be done with another software than
AWK, why I used this, OK, I know a bit only AWK and Bash scripting,
and if you know another and more effective method than this one, do
not hesitate and offer your suggestion.
Something like this in bash:
N=1
for X in "*$1*.png" ; do
Isn't it *"$1"*.png
Don't know for sure - I think they're both the same or the same under
most conditions, at least.
echo cp "$X" "$(printf '%02d_%s.png' N "$1" )"
N = $((N+1))
and N=$((N+1)) ?
Yes, no spaces is correct - I so rarely code in bash I forget and want
to pretty-print it!
My quoting on
echo cp "$X" "$(printf '%02d_%s.png' N "$1" )"
is wrong as well: the double quotes around $1 need escaping.
course I noticed about 2 seconds after pressing the send button.) :-(
done
Remove the 'echo' when you've finished testing!
On 9/22/2021 8:33 PM, Bruce Horrocks wrote:
On 23/09/2021 00:06, Janis Papanagnou wrote:
On 23.09.21 00:02, Bruce Horrocks wrote:
On 21/09/2021 04:33, Mohsen Owzar wrote:
Hi Guys
I'm working with Cygwin under Windows 10
I have written a program in AWK / Sh to copy or rename some selected >>>>> files, filtered by a string given by the user, from 01 to the length >>>>> of the selected files by the filter.
Before you say that this task can be done with another software than >>>>> AWK, why I used this, OK, I know a bit only AWK and Bash scripting,
and if you know another and more effective method than this one, do
not hesitate and offer your suggestion.
Something like this in bash:
N=1
for X in "*$1*.png" ; do
Isn't it *"$1"*.png
Don't know for sure - I think they're both the same or the same under
most conditions, at least.
No, Janis is correct. The first form will set X to the one literal
string "*$1*.png" while the second will populate it iteratively with the
name of every file in your directory whose name contains whatever value
$1 is and ends in .png.
My quoting on
echo cp "$X" "$(printf '%02d_%s.png' N "$1" )"
is wrong as well: the double quotes around $1 need escaping.
No, you can (and should in this case) nest double quotes within command substitution using $(...). If your intent was to get double quotes in
the output of the printf then you'd write it as
printf '%02d_"%s".png' N "$1"
not:
printf '%02d_%s.png' N \"$1\"
Bruce Horrocks schrieb am Donnerstag, 23. September 2021 um 20:50:23 UTC+2:
On 23/09/2021 13:38, Ed Morton wrote:
On 9/22/2021 8:33 PM, Bruce Horrocks wrote:Hm. The perils of testing on the command line instead of in a script.
On 23/09/2021 00:06, Janis Papanagnou wrote:
On 23.09.21 00:02, Bruce Horrocks wrote:
On 21/09/2021 04:33, Mohsen Owzar wrote:
Hi Guys
I'm working with Cygwin under Windows 10
I have written a program in AWK / Sh to copy or rename some selected >>>>>>> files, filtered by a string given by the user, from 01 to the length >>>>>>> of the selected files by the filter.
Before you say that this task can be done with another software than >>>>>>> AWK, why I used this, OK, I know a bit only AWK and Bash scripting, >>>>>>> and if you know another and more effective method than this one, do >>>>>>> not hesitate and offer your suggestion.
Something like this in bash:
N=1
for X in "*$1*.png" ; do
Isn't it *"$1"*.png
Don't know for sure - I think they're both the same or the same under
most conditions, at least.
No, Janis is correct. The first form will set X to the one literal
string "*$1*.png" while the second will populate it iteratively with the >>> name of every file in your directory whose name contains whatever value
$1 is and ends in .png.
You're right but it does look a bit counter-intuitive.
[snip]
I meant the \"$1\" ... but quotes in the output of the $() turned out toMy quoting on
echo cp "$X" "$(printf '%02d_%s.png' N "$1" )"
is wrong as well: the double quotes around $1 need escaping.
No, you can (and should in this case) nest double quotes within command
substitution using $(...). If your intent was to get double quotes in
the output of the printf then you'd write it as
printf '%02d_"%s".png' N "$1"
not:
printf '%02d_%s.png' N \"$1\"
be simpler than I thought because "$()" does the right thing.
So I think taking it all together we get:
#! /bin/bash
N=1
for X in *"$1"* ; do
echo cp "$X" "$(printf '%02d_%s.png' "$N" "$1")"
N=$((N+1))
done
which works for a limited test set on my desktop!
--
Bruce Horrocks
Surrey, England
I'm somehow confused.
As I understood, I can replace my whole code
including Shell, AWK, Shell
with only this following Shell lines:
:::::::::::::::::::::::::::::::::::::::::::::::::
#! /bin/bash
# Filename: Rename_Test.sh
N=1
for X in *"$1"*
do
echo cp "$X" "$(printf '%02d_%s.png' "$N" "$1")"
N=$((N+1))
done
:::::::::::::::::::::::::::::::::::::::::::::::::
When I run this script, I get the following error message: :::::::::::::::::::::::::::::::::::::::::::::::::
ll/cygdrive/c/Users/m.owzar/Desktop/GUI_View Screenshots/00_09
insgesamt 208
drwx------+ 1 m.owzar Domain Users 0 24. Sep 13:16 .
drwx------+ 1 m.owzar Domain Users 0 24. Sep 09:47 ..
-rwx------+ 1 m.owzar Domain Users 25272 21. Sep 03:47 '2021-09-21 03_47_09-no title.png'
-rwx------+ 1 m.owzar Domain Users 24687 21. Sep 03:47 '2021-09-21 03_47_18-no title.png'
-rwx------+ 1 m.owzar Domain Users 24774 21. Sep 03:47 '2021-09-21 03_47_23-no title.png'
-rwx------+ 1 m.owzar Domain Users 27528 21. Sep 03:47 '2021-09-21 03_47_41-no title.png'
-rwx------+ 1 m.owzar Domain Users 27937 21. Sep 03:47 '2021-09-21 03_47_45-no title.png'
-rwx------+ 1 m.owzar Domain Users 27559 21. Sep 03:47 '2021-09-21 03_47_50-no title.png'
-rwx------+ 1 m.owzar Domain Users 26617 21. Sep 03:48 '2021-09-21 03_48_01-no title.png'
m.owzar@ /cygdrive/c/Users/m.owzar/Desktop/GUI_View Screenshots/00_09
Rename_Test.sh "no title"/home/m.owzar/BinMo/Rename_Test.sh: Zeile 5: Syntaxfehler beim unerwarteten Wort `$'do\r''
'home/m.owzar/BinMo/Rename_Test.sh: Zeile 5: `do :::::::::::::::::::::::::::::::::::::::::::::::::
I use under Windows 10 the Cygwin, and in there,
my script at the end of each line has CR|LF.
I think for the given string as argument, it is valid also.
Due to this issue in my AWK part of the code,
I had to subtract one character from the given argument.
Does anyone know what this error message mean? (translated in English)
Line 5: Syntax error at unexpected Word `$'do\r''
Regards
Mohsen
On 23/09/2021 13:38, Ed Morton wrote:
On 9/22/2021 8:33 PM, Bruce Horrocks wrote:
On 23/09/2021 00:06, Janis Papanagnou wrote:
On 23.09.21 00:02, Bruce Horrocks wrote:
On 21/09/2021 04:33, Mohsen Owzar wrote:
Hi Guys
I'm working with Cygwin under Windows 10
I have written a program in AWK / Sh to copy or rename some selected >>>>> files, filtered by a string given by the user, from 01 to the length >>>>> of the selected files by the filter.
Before you say that this task can be done with another software than >>>>> AWK, why I used this, OK, I know a bit only AWK and Bash scripting, >>>>> and if you know another and more effective method than this one, do >>>>> not hesitate and offer your suggestion.
Something like this in bash:
N=1
for X in "*$1*.png" ; do
Isn't it *"$1"*.png
Don't know for sure - I think they're both the same or the same under
most conditions, at least.
No, Janis is correct. The first form will set X to the one literalHm. The perils of testing on the command line instead of in a script.
string "*$1*.png" while the second will populate it iteratively with the name of every file in your directory whose name contains whatever value
$1 is and ends in .png.
You're right but it does look a bit counter-intuitive.
[snip]
My quoting on
echo cp "$X" "$(printf '%02d_%s.png' N "$1" )"
is wrong as well: the double quotes around $1 need escaping.
No, you can (and should in this case) nest double quotes within command substitution using $(...). If your intent was to get double quotes in
the output of the printf then you'd write it as
printf '%02d_"%s".png' N "$1"
not:
printf '%02d_%s.png' N \"$1\"I meant the \"$1\" ... but quotes in the output of the $() turned out to
be simpler than I thought because "$()" does the right thing.
So I think taking it all together we get:
#! /bin/bash
N=1
for X in *"$1"* ; do
echo cp "$X" "$(printf '%02d_%s.png' "$N" "$1")"
N=$((N+1))
done
which works for a limited test set on my desktop!
--
Bruce Horrocks
Surrey, England
ll/cygdrive/c/Users/m.owzar/Desktop/GUI_View Screenshots/00_09
Rename_Test.sh "no title"/home/m.owzar/BinMo/Rename_Test.sh: Zeile 5: Syntaxfehler beim unerwarteten Wort `$'do\r''
Bruce Horrocks schrieb am Donnerstag, 23. September 2021 um 20:50:23 UTC+2:
On 23/09/2021 13:38, Ed Morton wrote:
On 9/22/2021 8:33 PM, Bruce Horrocks wrote:Hm. The perils of testing on the command line instead of in a script.
On 23/09/2021 00:06, Janis Papanagnou wrote:
On 23.09.21 00:02, Bruce Horrocks wrote:
On 21/09/2021 04:33, Mohsen Owzar wrote:
Hi Guys
I'm working with Cygwin under Windows 10
I have written a program in AWK / Sh to copy or rename some selected >>>>>>> files, filtered by a string given by the user, from 01 to the length >>>>>>> of the selected files by the filter.
Before you say that this task can be done with another software than >>>>>>> AWK, why I used this, OK, I know a bit only AWK and Bash scripting, >>>>>>> and if you know another and more effective method than this one, do >>>>>>> not hesitate and offer your suggestion.
Something like this in bash:
N=1
for X in "*$1*.png" ; do
Isn't it *"$1"*.png
Don't know for sure - I think they're both the same or the same under
most conditions, at least.
No, Janis is correct. The first form will set X to the one literal
string "*$1*.png" while the second will populate it iteratively with the >>> name of every file in your directory whose name contains whatever value
$1 is and ends in .png.
You're right but it does look a bit counter-intuitive.
[snip]
I meant the \"$1\" ... but quotes in the output of the $() turned out toMy quoting on
echo cp "$X" "$(printf '%02d_%s.png' N "$1" )"
is wrong as well: the double quotes around $1 need escaping.
No, you can (and should in this case) nest double quotes within command
substitution using $(...). If your intent was to get double quotes in
the output of the printf then you'd write it as
printf '%02d_"%s".png' N "$1"
not:
printf '%02d_%s.png' N \"$1\"
be simpler than I thought because "$()" does the right thing.
So I think taking it all together we get:
#! /bin/bash
N=1
for X in *"$1"* ; do
echo cp "$X" "$(printf '%02d_%s.png' "$N" "$1")"
N=$((N+1))
done
which works for a limited test set on my desktop!
--
Bruce Horrocks
Surrey, England
I'm somehow confused.
As I understood, I can replace my whole code
including Shell, AWK, Shell
with only this following Shell lines:
:::::::::::::::::::::::::::::::::::::::::::::::::
#! /bin/bash
# Filename: Rename_Test.sh
N=1
for X in *"$1"*
do
echo cp "$X" "$(printf '%02d_%s.png' "$N" "$1")"
N=$((N+1))
done
:::::::::::::::::::::::::::::::::::::::::::::::::
When I run this script, I get the following error message: :::::::::::::::::::::::::::::::::::::::::::::::::
ll/cygdrive/c/Users/m.owzar/Desktop/GUI_View Screenshots/00_09
insgesamt 208
drwx------+ 1 m.owzar Domain Users 0 24. Sep 13:16 .
drwx------+ 1 m.owzar Domain Users 0 24. Sep 09:47 ..
-rwx------+ 1 m.owzar Domain Users 25272 21. Sep 03:47 '2021-09-21 03_47_09-no title.png'
-rwx------+ 1 m.owzar Domain Users 24687 21. Sep 03:47 '2021-09-21 03_47_18-no title.png'
-rwx------+ 1 m.owzar Domain Users 24774 21. Sep 03:47 '2021-09-21 03_47_23-no title.png'
-rwx------+ 1 m.owzar Domain Users 27528 21. Sep 03:47 '2021-09-21 03_47_41-no title.png'
-rwx------+ 1 m.owzar Domain Users 27937 21. Sep 03:47 '2021-09-21 03_47_45-no title.png'
-rwx------+ 1 m.owzar Domain Users 27559 21. Sep 03:47 '2021-09-21 03_47_50-no title.png'
-rwx------+ 1 m.owzar Domain Users 26617 21. Sep 03:48 '2021-09-21 03_48_01-no title.png'
m.owzar@ /cygdrive/c/Users/m.owzar/Desktop/GUI_View Screenshots/00_09
Rename_Test.sh "no title"/home/m.owzar/BinMo/Rename_Test.sh: Zeile 5: Syntaxfehler beim unerwarteten Wort `$'do\r''
'home/m.owzar/BinMo/Rename_Test.sh: Zeile 5: `do :::::::::::::::::::::::::::::::::::::::::::::::::
I use under Windows 10 the Cygwin, and in there,
my script at the end of each line has CR|LF.
I think for the given string as argument, it is valid also.
Due to this issue in my AWK part of the code,
I had to subtract one character from the given argument.
Does anyone know what this error message mean? (translated in English)
Line 5: Syntax error at unexpected Word `$'do\r''
Regards
Mohsen
On 24.09.21 13:50, Mohsen Owzar wrote:
Bruce Horrocks schrieb am Donnerstag, 23. September 2021 um 20:50:23 UTC+2: >>> On 23/09/2021 13:38, Ed Morton wrote:
On 9/22/2021 8:33 PM, Bruce Horrocks wrote:Hm. The perils of testing on the command line instead of in a script.
On 23/09/2021 00:06, Janis Papanagnou wrote:
On 23.09.21 00:02, Bruce Horrocks wrote:
On 21/09/2021 04:33, Mohsen Owzar wrote:
Hi Guys
I'm working with Cygwin under Windows 10
I have written a program in AWK / Sh to copy or rename some selected >>>>>>>> files, filtered by a string given by the user, from 01 to the length >>>>>>>> of the selected files by the filter.
Before you say that this task can be done with another software than >>>>>>>> AWK, why I used this, OK, I know a bit only AWK and Bash scripting, >>>>>>>> and if you know another and more effective method than this one, do >>>>>>>> not hesitate and offer your suggestion.
Something like this in bash:
N=1
for X in "*$1*.png" ; do
Isn't it *"$1"*.png
Don't know for sure - I think they're both the same or the same under >>>>> most conditions, at least.
No, Janis is correct. The first form will set X to the one literal
string "*$1*.png" while the second will populate it iteratively with the >>>> name of every file in your directory whose name contains whatever value >>>> $1 is and ends in .png.
You're right but it does look a bit counter-intuitive.
[snip]
I meant the \"$1\" ... but quotes in the output of the $() turned out to >>> be simpler than I thought because "$()" does the right thing.My quoting on
echo cp "$X" "$(printf '%02d_%s.png' N "$1" )"
is wrong as well: the double quotes around $1 need escaping.
No, you can (and should in this case) nest double quotes within command >>>> substitution using $(...). If your intent was to get double quotes in
the output of the printf then you'd write it as
printf '%02d_"%s".png' N "$1"
not:
printf '%02d_%s.png' N \"$1\"
So I think taking it all together we get:
#! /bin/bash
N=1
for X in *"$1"* ; do
echo cp "$X" "$(printf '%02d_%s.png' "$N" "$1")"
N=$((N+1))
done
which works for a limited test set on my desktop!
--
Bruce Horrocks
Surrey, England
I'm somehow confused.
As I understood, I can replace my whole code
including Shell, AWK, Shell
with only this following Shell lines:
:::::::::::::::::::::::::::::::::::::::::::::::::
#! /bin/bash
# Filename: Rename_Test.sh
N=1
for X in *"$1"*
do
echo cp "$X" "$(printf '%02d_%s.png' "$N" "$1")"
N=$((N+1))
done
:::::::::::::::::::::::::::::::::::::::::::::::::
When I run this script, I get the following error message:
:::::::::::::::::::::::::::::::::::::::::::::::::
ll/cygdrive/c/Users/m.owzar/Desktop/GUI_View Screenshots/00_09
insgesamt 208
drwx------+ 1 m.owzar Domain Users 0 24. Sep 13:16 .
drwx------+ 1 m.owzar Domain Users 0 24. Sep 09:47 ..
-rwx------+ 1 m.owzar Domain Users 25272 21. Sep 03:47 '2021-09-21 03_47_09-no title.png'
-rwx------+ 1 m.owzar Domain Users 24687 21. Sep 03:47 '2021-09-21 03_47_18-no title.png'
-rwx------+ 1 m.owzar Domain Users 24774 21. Sep 03:47 '2021-09-21 03_47_23-no title.png'
-rwx------+ 1 m.owzar Domain Users 27528 21. Sep 03:47 '2021-09-21 03_47_41-no title.png'
-rwx------+ 1 m.owzar Domain Users 27937 21. Sep 03:47 '2021-09-21 03_47_45-no title.png'
-rwx------+ 1 m.owzar Domain Users 27559 21. Sep 03:47 '2021-09-21 03_47_50-no title.png'
-rwx------+ 1 m.owzar Domain Users 26617 21. Sep 03:48 '2021-09-21 03_48_01-no title.png'
m.owzar@ /cygdrive/c/Users/m.owzar/Desktop/GUI_View Screenshots/00_09
Rename_Test.sh "no title"/home/m.owzar/BinMo/Rename_Test.sh: Zeile 5: Syntaxfehler beim unerwarteten Wort `$'do\r''
'home/m.owzar/BinMo/Rename_Test.sh: Zeile 5: `do
:::::::::::::::::::::::::::::::::::::::::::::::::
I use under Windows 10 the Cygwin, and in there,
my script at the end of each line has CR|LF.
I think for the given string as argument, it is valid also.
Due to this issue in my AWK part of the code,
I had to subtract one character from the given argument.
Does anyone know what this error message mean? (translated in English)
Line 5: Syntax error at unexpected Word `$'do\r''
It means that there's a CR that shouldn't be there. Actually saying
what you mentioned above, that it's a DOS file. But that is illegal.
If you cleanup your script (remove the CRs and keep the LFs only)
that error should vanish.
Also the script has a bug: if the parameter text doesn't match any file
then the script tries to copy a non-existent file and fails. The
solution is to test that the file exists first before trying to copy it.
:::::::::::::
#! /bin/bash
# Filename: Rename_Test.sh
N=1
for X in *"$1"*
do
if [ -f "$X" ]
then
echo cp "$X" "$(printf '%02d_%s.png' "$N" "$1")"
N=$((N+1))
fi
done
:::::::::::::
Also the script has a bug: if the parameter text doesn't match any file
then the script tries to copy a non-existent file and fails. The
solution is to test that the file exists first before trying to copy it.
In article <d2dfb313-e993-b4a9...@scorecrow.com>,Thanks alot to all for their contributions.
Bruce Horrocks <07....@scorecrow.com> wrote:
...
Also the script has a bug: if the parameter text doesn't match any file >then the script tries to copy a non-existent file and fails. The
solution is to test that the file exists first before trying to copy it. Another way is to use bash's nullglob option.
I've always thought the way sh type shells handle "no file" globbing was wrong (*). That nullglob should have been the default from the get-go.
Of course, the default can't be changed now, but going forward, most bash code should set nullglob.
(*) Note that csh changed this - which was (and is) a Good Thing, but it is different from either classic sh or bash-with-nullglob.
P.S. This thread needs to be moved to the shell group. It has nothing to
do with AWK.
--
People sleep peaceably in their beds at night only because rough
men stand ready to do violence on their behalf.
George Orwell
On Saturday, 25 September 2021 at 12:48:42 UTC-4, Mohsen Owzar wrote:
Due to what is said above, I have to convert (dos2unix) all my scripts, written with notepad++Mohsen, Two points. A) Notepad++ offers macro recording to speed up converting and has a command to save
under Windows, to be able to run under Cygwin.
Regards
Mohsen
LF=unix, even if the file came in as dos/win and vice versa.
B) dos2unix: I tried Gawk under Cygwin and switched to Win 10 WSL2 running Ubuntu. Win batch files can run unix commands
using unix or windows file paths, e.g. in file from win10, out file to unix. Bash scripts can call Win 10 commands
& programs and access windows and/or unix files. Or pure unix or pure win. I like the flexibility to use what I need.
File i/o BETWEEN unix and win 10 is a little slower, maybe 10%. Gawk unix is 64-bit and runs some .awks 50% faster
for cpu intensive, 15% slower for just huge memory, not much cpu, and I suspect 20%, maybe 50%, slower for lots of
file i/o. So, Gawk under WSL is about the same as under Cygwin. I like both for 64-bit memory for large arrays and
WSL for file system "interoperability"
-just FYIO, john
Due to what is said above, I have to convert (dos2unix) all my scripts, written with notepad++
under Windows, to be able to run under Cygwin.
Regards
Mohsen
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 499 |
Nodes: | 16 (2 / 14) |
Uptime: | 53:48:43 |
Calls: | 9,839 |
Files: | 13,764 |
Messages: | 6,194,480 |
Posted today: | 1 |