Hi all, i have more questions but its all about variables so lets begin
1st, is possible to list all variables ?
Command env display only shell variables, but what if i declared another variable? Command set display more variables but not defined by me.
2nd, what difference is between set and env ?
3rd, lets say i declared variable which contains whitespaces, or i have variable which contains whitespaces for example IFS. When i type echo $IFS i see nothing, so i dont know if is declared or if contain white spaces.
Is possible to write white spaces in C format (\n \r \t ...) ?
4th what is difference between sourcing and exporting variable ?
PS: i saw declaration of IFS in set command (IFS=$' \t\n')
Thanks for reply i am trying to do for loop in bash script throught *.mp3 files. Problem is that the mp3 files contains white spaces so i found this advice
You dont have to set IFS for that I think.
All that you need is to quote the variable.
It will be easier if you post the code here.
Dont echo $IFS
Just give the command as mentioned below to list it out.
yes i can do that this way but if i want to use mv or cp in my script how do this. Thes works because of sed's & which is actual line. But i am interesting how to force bash for loop just skipping to new value just after recieving new line not any whitespace
Always double check before you click on "Submit Reply".
Check if there are mistakes.
Make sure others can understand what you are trying to say.
Write shorter sentences.
If possible, post the input and the expected output.
If your requirements have changed, mention that.
Can you please clearly state what you want?
I am not sure if this is what you are looking for:
ls -1 s* | sed 's/.*/cp "&" "&.cpy"/' | ksh
All files starting with a "s" will be copied with extension ".cpy"
I am not sure why you need a loop.
I am sorry if i typed something which is un clearly. I have followings files in directory
-rw-r--r-- 1 marek marek 0 2009-07-31 20:37 01 - album - track001.mp3
-rw-r--r-- 1 marek marek 0 2009-07-31 20:37 02 - album - track002.mp3
-rw-r--r-- 1 marek marek 0 2009-07-31 20:37 03 - album - track003.mp3
-rw-r--r-- 1 marek marek 0 2009-07-31 20:37 04 - album - track004.mp3
-rw-r--r-- 1 marek marek 0 2009-07-31 20:37 05 - album - track005.mp3
-rw-r--r-- 1 marek marek 0 2009-07-31 20:37 06 - album - track006.mp3
-rw-r--r-- 1 marek marek 0 2009-07-31 20:37 07 - album - track007.mp3
-rw-r--r-- 1 marek marek 0 2009-07-31 20:37 08 - album - track008.mp3
-rw-r--r-- 1 marek marek 0 2009-07-31 20:37 09 - album - track009.mp3
what i am trying to do is rename to trackXXX.mp3
I found that can be done with
for jmeno in *.mp3
do
cp $jmeno /tmp/`echo $i | sed "s/\([0-9]\{2\}\).*/track0\1.mp3/"`
done
but did not work for me. Also i searched and found that i have to change IFS variable.
Can you please advise me if this task can be done using using bash and for loop (not ksh), i am traying to learn something about bash scripting. Thanks
Print global(=environment) variables. Child process get copy of global variables, but not locals.
env
What is locals ? set - env
How to make global variable ? Use export command.
export variable
How to remove variable ?
unset variable
How to look ex. IFS value, IFS include some special characters ? Example:
echo -n "$IFS" | od -c
IFS default value is "white space" = space+tab+newline
Using special characters. Example IFS is TAB+backspace+hex 02+newline
IFS=$'\t\b\x02\n'
One solution for your needs =remove all chars before ex. track007.mp3
= remove "07 - album - ". Space before string track.
#!/bin/ksh
#!/bin/bash
for f in *.mp3
do
# remove all chars before last space including last space
new="${f##* }"
[ "$new" != "$f" ] && mv "$f" "$new"
done
cp: target `track001.mp3' is not a directory
cp: target `track002.mp3' is not a directory
cp: target `track003.mp3' is not a directory
cp: target `track004.mp3' is not a directory
cp: target `track005.mp3' is not a directory
cp: target `track006.mp3' is not a directory
cp: target `track007.mp3' is not a directory
cp: target `track008.mp3' is not a directory
cp: target `track009.mp3' is not a directory
thanks anyway
I am trying something more bit complicated:
#!/bin/bash
#IFS=$'\n'
echo default IFS:
echo -n "$IFS" | od -a -b
IFS=$'\n'
echo new IFS:
echo -n "$IFS" | od -a -b
for x in `ls *.mp3`
do
echo "`sed -ne 's/\([0-9]\{2\}\) - .* - .*[0-9]\{3\}\.mp3/track\1.mp3/gp' $x`"
done
echo new IFS:
echo -n "$IFS" | od -a -b
unset IFS
echo standard IFS:
echo -n "$IFS" | od -a -b
but alsdo did not work, IFS is set to newline, sed command must be OK because if i try it outside script everything works. Now i tryed just echo output of sed but nothing appears.
ksh[1]: 01: not found [No such file or directory]
ksh[2]: 02: not found [No such file or directory]
ksh[3]: 03: not found [No such file or directory]
ksh[4]: 04: not found [No such file or directory]
ksh[5]: 05: not found [No such file or directory]
ksh[6]: 06: not found [No such file or directory]
ksh[7]: 07: not found [No such file or directory]
ksh[8]: 08: not found [No such file or directory]
ksh[9]: 09: not found [No such file or directory]
Thanks anyway.
I am very interesting what is wrong with my script, for and sed works but when i put it together it does nothing.
Not that it affects the command in any way, but yes, the "-1" is not required.
I generally prefer 1 liner to run on the prompt and most of the requests are for that.
But, I do like your approach too.
---------- Post updated at 09:51 PM ---------- Previous update was at 09:37 PM ----------
Are you sure you gave the quotes in the "&"?
Ok. First try this and see what comes out:
ls *track* | sed 's/.*\(track.*$\)/cp "&" \1/'
If that looks good then try this:
ls *track* | sed 's/.*\(track.*$\)/cp "&" \1/' | ksh
But if you are not looking for an one liner, then the codeby Reborg looks more simpler.