shell variables advanced


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting shell variables advanced
# 15  
Old 08-01-2009
Print all variables - local and global.
Code:
set

Print global(=environment) variables. Child process get copy of global variables, but not locals.
Code:
env

What is locals ? set - env

How to make global variable ? Use export command.
Code:
export variable

How to remove variable ?
Code:
unset variable

How to look ex. IFS value, IFS include some special characters ? Example:
Code:
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
Code:
IFS=$'\t\b\x02\n'


One solution for your needs =remove all chars before ex. track007.mp3
= remove "07 - album - ". Space before string track.
Code:
#!/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


Last edited by kshji; 08-01-2009 at 10:08 AM..
# 16  
Old 08-01-2009
Quote:
Originally Posted by Franklin52
Something like this?

Code:
ls *.mp3 | awk '{system("cp " $0 " " $NF)}'

I've used the cp command but you can replace it with the mv command.

Regards
Did not work for me
Code:
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:
Code:
#!/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.

also tried replace echo with
Code:
cp "$x" "`sed -ne 's/\([0-9]\{2\}\) - .* - .*[0-9]\{3\}\.mp3/track\1.mp3/gp' $x`"

but according expectation it do nothing.

Also i am noticed something strange
Code:
numbers="$(printf "\n")"; echo -n "$numbers" | od -a -b
0000000

numbers="$(printf "\t")"; echo -n "$numbers" | od -a -b
0000000  ht
        011
0000001

Can someone explain that ???


PS i am total bash scripting newbie, sorry for dummmy questions

Last edited by wakatana; 08-01-2009 at 02:02 PM..
# 17  
Old 08-01-2009
If all that you are trying to do is just rename the file,
then this is a simple and straight solution.
Code:
ls -1 | sed 's/.*\(track.*$\)/cp "&" \1/' | ksh

Doesthis solve your issue?
Or is it necessary it has to be done your way?
# 18  
Old 08-01-2009
I tried that but did not work this is error:
Code:
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.
# 19  
Old 08-01-2009
Quote:
Originally Posted by edidataguy
Code:
ls -1 | sed 's/.*\(track.*$\)/cp "&" \1/' | ksh

The -1 as an argument to ls is pointless if you are piping the result.

Anyway:
Code:
for file in *.mp3 ; do
      mv "$file" "${file##* }"
done

# 20  
Old 08-01-2009
Quote:
Originally Posted by reborg
The -1 as an argument to ls is pointless if you are piping the result.

Anyway:
Code:
for file in *.mp3 ; do
      mv "$file" "${file##* }"
done

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 ----------

Quote:
Originally Posted by edidataguy
If all that you are trying to do is just rename the file,
then this is a simple and straight solution.
Code:
ls -1 | sed 's/.*\(track.*$\)/cp "&" \1/' | ksh

Doesthis solve your issue?
Or is it necessary it has to be done your way?
Are you sure you gave the quotes in the "&"?
Ok. First try this and see what comes out:
Code:
ls *track* | sed 's/.*\(track.*$\)/cp "&" \1/'

If that looks good then try this:
Code:
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.
# 21  
Old 08-02-2009
Reborg's solution works, thanks.
Edidataguy yes i type it properly (use copy and paste) but did not work, neither the
Code:
ls *track* | sed 's/.*\(track.*$\)/cp "&" \1/'

did not work.
But can somebody explain why my bash script with for did not work ? I am trying to learn something new in bash not ksh. thanks

Last edited by wakatana; 08-02-2009 at 09:43 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to write config shell script to pass variables in master shell script?

Dear Unix gurus, We have a config shell script file which has 30 variables which needs to be passed to master unix shell script that invokes oracle database sessions. So those 30 variables need to go through the database sessions (They are inputs) via a shell script. one of the variable name... (1 Reply)
Discussion started by: dba1981
1 Replies

2. UNIX for Dummies Questions & Answers

How to write Config shell script to pass variables in master shell script?

Dear Unix gurus, We have a config shell script file which has 30 variables which needs to be passed to master unix shell script that invokes oracle database sessions. So those 30 variables need to go through the database sessions (They are inputs) via a shell script. one of the variable name... (1 Reply)
Discussion started by: dba1981
1 Replies

3. Shell Programming and Scripting

awk - take variables out to shell

Hi, How could we take the value of awk variables out to shell? I know the following methods 1. awk '{print $1}' < file | read a echo $a 2. a=`awk '{print $1}' < file` echo $a Please let me know if there are any other methods. Also, how do we take more than 1 variable value... (4 Replies)
Discussion started by: Thumban
4 Replies

4. Homework & Coursework Questions

Trouble with Advanced Shell Programming

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: I am working on a hands on project. We are creating a script for a corporate phone list. The project I am... (2 Replies)
Discussion started by: SarahBelle7858
2 Replies

5. Shell Programming and Scripting

Shell advanced syntax?

I am not an expert of shell scripting, but I can do some simple things. Now, I read a script written by others and I need some help from the experts of this forum. Please help me to understand what is going on in this cycle: if ; then ] && \ export... (25 Replies)
Discussion started by: alt
25 Replies

6. Shell Programming and Scripting

Advanced error handling in shell scripts

Hi all I've got a question regarding error handling in shell scripts. My background is mainly object oriented programming languages, but for a year or so I've been doing more and more (bash) shell scripting (which I quite enjoy by the way). To handle errors in my scripts I... (3 Replies)
Discussion started by: script_man
3 Replies

7. Shell Programming and Scripting

$0 shell variables

Would appreciate if someone can explain the ${0##*/} line. What does it do? I am aware that $0 is the script name, $# is number of arguments passed in, $* is all the arguments. With the curly brackets {} added in, what's the eventual effect? Does ${0##*/} actually equals $0$#$*? (something like... (3 Replies)
Discussion started by: new2ss
3 Replies

8. Post Here to Contact Site Administrators and Moderators

Where can I download the VTC - Unix Shell Scripting Advanced complete video

Where can I download the VTC - Unix Shell Scripting Advanced complete video. I don't know in which thread I should post this question.Plz help me out, or just tell me the link in the reply to this post. Thanks in advance. (0 Replies)
Discussion started by: villain41
0 Replies

9. Shell Programming and Scripting

variables in shell

hi, i'm new in shell scripting and i'm working on bash on solaris 5.9 after try many stuff with unexpected results, i wonder: it is not posible in bash, to use a variable that was created inside a loop, out of it? i mean, for instance: cat mytext | \ while read text do viko=$text... (2 Replies)
Discussion started by: viko
2 Replies

10. Shell Programming and Scripting

Using shell variables In awk

Oh its not my day for syntax... cat gzipsize.txt | awk '{print "echo",$1,$2} > master.txt I have read a lot about the awk -v but haven't been able to get it to work. I have a variable in my script and I'm looking just to push it into the awk after the $2 (or anywhere would do)!!! Every... (11 Replies)
Discussion started by: nortypig
11 Replies
Login or Register to Ask a Question