[SLE] Shell Programing
I am working on my first shell script (whoo hoo!) and I have run in to a small problem. I need to assign a variable, say x, to = 000001 with the leading zeros then advance this variable by 1, ie. 000002, 000003, etc. Is this possible in a bash script? I also need to slice a string and then test the result case insisitive. In case it is important this gem will, over the network, take a group of directories and rename all the files starting with 000001 and the proper extention (the files are on a Win98 machine). Thanks for your help. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HEAD> <META content="text/html; charset=iso-8859-1" http-equiv=Content-Type> <META content="MSHTML 5.00.2614.3500" name=GENERATOR> <STYLE></STYLE> </HEAD> <BODY bgColor=#ffffff> <DIV><FONT face=Arial size=2>I am working on my first shell script (whoo hoo!) and I have run in to a small problem. I need to assign a variable, say x, to = 000001 with the leading zeros then advance this variable by 1, ie. 000002, 000003, etc. Is this possible in a bash script? I also need to slice a string and then test the result case insisitive. In case it is important this gem will, over the network, take a group of directories and rename all the files starting with 000001 and the proper extention (the files are on a Win98 machine). Thanks for your help.</FONT></DIV> <DIV> </DIV></BODY>
Hi, On Mon, Dec 27, 1999 at 09:45 -0600, Patrick K Moorman wrote:
I am working on my first shell script (whoo hoo!) and I have run in to a small problem. I need to assign a variable, say x, to = 000001 with the leading zeros then advance this variable by 1, ie. 000002, 000003, etc. Is this possible in a
I don't know if this is possible, probably not. But you can use printf "%06d\n", $x to pad a number with leading zeros.
bash script? I also need to slice a string and then test the result case insisitive. In case it is important this gem will, over the network, take a group of directories and rename all the files starting with 000001 and the proper extention (the files are on a Win98 machine). Thanks for your help.
Why don't you post an example of what you're trying to achive? This would make it much easier to help you. Ciao, Stefan -- To unsubscribe send e-mail to suse-linux-e-unsubscribe@suse.com For additional commands send e-mail to suse-linux-e-help@suse.com Also check the FAQ at http://www.suse.com/Support/Doku/FAQ/
I have a directory, say c:\top, that has sub directories c:\top\001, c:\top\002, etc. I want to run a script from c:\top (mounted as /smb on Linbox) that will go through the subdirectories renaming each file starting with 000001. I have the script written except that it starts with 1 and goes up. I also need the files to retain their extensions so I had forseen something to read the file name, slice the ext off and then reattach it, ie. abcdef.txt=>000001.txt. I am attaching what I have so far. I am making this up as I go with one eye on 'Beginning Linux Programing' from Wrox Press. Thanks for the help. Stefan Troeger wrote:
Hi,
On Mon, Dec 27, 1999 at 09:45 -0600, Patrick K Moorman wrote:
I am working on my first shell script (whoo hoo!) and I have run in to a small problem. I need to assign a variable, say x, to = 000001 with the leading zeros then advance this variable by 1, ie. 000002, 000003, etc. Is this possible in a
I don't know if this is possible, probably not. But you can use
printf "%06d\n", $x
to pad a number with leading zeros.
bash script? I also need to slice a string and then test the result case insisitive. In case it is important this gem will, over the network, take a group of directories and rename all the files starting with 000001 and the proper extention (the files are on a Win98 machine). Thanks for your help.
Why don't you post an example of what you're trying to achive? This would make it much easier to help you.
Ciao, Stefan
-- To unsubscribe send e-mail to suse-linux-e-unsubscribe@suse.com For additional commands send e-mail to suse-linux-e-help@suse.com Also check the FAQ at http://www.suse.com/Support/Doku/FAQ/
-- In any case, as human beings, it is essential for each of us to cultivate and polish our individual path. Miyamoto Musashi 1643 AD #! /bin/bash x=000001 for directories in * do if -d $directories then for files in $directories/* do if -f $files then mv $files $x x=$(($x+1)) fi done fi done -- To unsubscribe send e-mail to suse-linux-e-unsubscribe@suse.com For additional commands send e-mail to suse-linux-e-help@suse.com Also check the FAQ at http://www.suse.com/Support/Doku/FAQ/
Hi, On Mon, Dec 27, 1999 at 15:30 -0600, khadji@pld.com wrote:
I have a directory, say c:\top, that has sub directories c:\top\001, c:\top\002, etc. I want to run a script from c:\top (mounted as /smb on Linbox) that will go through the subdirectories renaming each file starting with 000001. I have the script written except that it starts with 1 and goes up. I also need the files to retain their extensions so I had forseen something to read the file name, slice the ext off and then reattach it, ie. abcdef.txt=>000001.txt. I am attaching what I have so far. I am making this up as I go with one eye on 'Beginning Linux Programing' from Wrox Press. Thanks for the help. [...] #! /bin/bash
x=000001
for directories in * do if -d $directories
This should be if [ -d "$directories" ]
then for files in $directories/* do if -f $files
if [ -f "$files" ]
then mv $files $x
Enclose the variable names in double quotes. Otherwise your script will fail on filenames with spaces: [sttr]/var/tmp/1> touch "a b" [sttr]/var/tmp/1> ls a b [sttr]/var/tmp/1> for x in *; do mv $x foo; done mv: when moving multiple files, last argument must be a directory [sttr]/var/tmp/1> ls a b [sttr]/var/tmp/1> for x in *; do mv "$x" foo; done [sttr]/var/tmp/1> ls foo
x=$(($x+1)) fi done fi done
Hmm. One problem with your script is that it won't descend into all subdirectories. If you had a directory c:\top\001\001, it would never be visited. To remedy this I'd use find to create a list of all files in the current directory and its subdirectories. #!/bin/sh i=1 for x in $(find . -type f); do mv "$x" "$(dirname $x)/$(printf "%06d" $i)$(echo $x |sed 's#.*\(\.\)#\1#g')" i=$((i+1)) done hould do it. The mv line may look a little tricky but it really isn't. Suppose $x contained ./top/001/abcdef.txt and $i was 5. Then dirname $x would give you the directory name ./top/001. printf "%06d" $i would output 000005. echo $x |sed 's#.*\(\.\)#\1#g' gives you the extenstion .txt. Finally everything is concatenated to ./top/001/000005.txt Ciao, Stefan -- To unsubscribe send e-mail to suse-linux-e-unsubscribe@suse.com For additional commands send e-mail to suse-linux-e-help@suse.com Also check the FAQ at http://www.suse.com/Support/Doku/FAQ/
Is there some where that I can get this information? I thank you for your help, I even understand some of it ;). ----- Original Message ----- From: Stefan Troeger <stefan.troeger@wirtschaft.tu-chemnitz.de> To: suse-linux-e <suse-linux-e@suse.com> Sent: Monday, December 27, 1999 6:35 PM Subject: Re: [SLE] Shell Programing
Hi,
On Mon, Dec 27, 1999 at 15:30 -0600, khadji@pld.com wrote:
I have a directory, say c:\top, that has sub directories c:\top\001, c:\top\002, etc. I want to run a script from c:\top (mounted as /smb on Linbox) that will go through the subdirectories renaming each file starting with 000001. I have the script written except that it starts with 1 and goes up. I also need the files to retain their extensions so I had forseen something to read the file name, slice the ext off and then reattach it, ie. abcdef.txt=>000001.txt. I am attaching what I have so far. I am making this up as I go with one eye on 'Beginning Linux Programing' from Wrox Press. Thanks for the help. [...] #! /bin/bash
x=000001
for directories in * do if -d $directories
This should be
if [ -d "$directories" ]
then for files in $directories/* do if -f $files
if [ -f "$files" ]
then mv $files $x
Enclose the variable names in double quotes. Otherwise your script will fail on filenames with spaces:
[sttr]/var/tmp/1> touch "a b" [sttr]/var/tmp/1> ls a b [sttr]/var/tmp/1> for x in *; do mv $x foo; done mv: when moving multiple files, last argument must be a directory [sttr]/var/tmp/1> ls a b [sttr]/var/tmp/1> for x in *; do mv "$x" foo; done [sttr]/var/tmp/1> ls foo
x=$(($x+1)) fi done fi done
Hmm. One problem with your script is that it won't descend into all subdirectories. If you had a directory c:\top\001\001, it would never be visited. To remedy this I'd use find to create a list of all files in the current directory and its subdirectories.
#!/bin/sh i=1 for x in $(find . -type f); do mv "$x" "$(dirname $x)/$(printf "%06d" $i)$(echo $x |sed 's#.*\(\.\)#\1#g')" i=$((i+1)) done
should do it. The mv line may look a little tricky but it really isn't. Suppose $x contained ./top/001/abcdef.txt and $i was 5. Then
dirname $x
would give you the directory name ./top/001.
printf "%06d" $i
would output 000005.
echo $x |sed 's#.*\(\.\)#\1#g'
gives you the extenstion .txt. Finally everything is concatenated to
./top/001/000005.txt
Ciao, Stefan
-- To unsubscribe send e-mail to suse-linux-e-unsubscribe@suse.com For additional commands send e-mail to suse-linux-e-help@suse.com Also check the FAQ at http://www.suse.com/Support/Doku/FAQ/
-- To unsubscribe send e-mail to suse-linux-e-unsubscribe@suse.com For additional commands send e-mail to suse-linux-e-help@suse.com Also check the FAQ at http://www.suse.com/Support/Doku/FAQ/
participants (2)
-
khadji@pld.com
-
stefan.troeger@wirtschaft.tu-chemnitz.de