Visit Our UNIX and Linux User Community


shell variables advanced


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting shell variables advanced
# 8  
Old 07-29-2009
Quote:
Originally Posted by wakatana
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
HTML Code:
https://www.unix.com/shell-programming-scripting/42839-preventing-whitespace-delimiter-loop-bash-sh.html
in short: back up IFS then set IFS to IFS=$'\n', but how can i print current value of IFS ? echo $IFS gives me nothing. Thanks
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.
Code:
 set | grep "^IFS"

# 9  
Old 07-30-2009
OK i put code here, just simple for loop and print line containing white spaces

Code:
#!/bin/bash
for i in $(ls | awk '{print "\""$0"\""}'); do
    echo -ne "file {$i}\n"
done

names with white spaces are printed like:
Code:
file{"one}
file{long}
file{name"}

i want:
Code:
file {"one long name"}

also tried to quote
Code:
ls | awk '{print "\""$0"\""}'

inf for loop but did not work according expectations. Also awk i tried i thought it may help with quotes but did not.
# 10  
Old 07-30-2009
Code:
 
ls -1 s* | sed 's/.*/file \{"&"\}/'

# 11  
Old 07-31-2009
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
# 12  
Old 07-31-2009
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:
Code:
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.
# 13  
Old 08-01-2009
I am sorry if i typed something which is un clearly. I have followings files in directory

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

Code:
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
# 14  
Old 08-01-2009
Quote:
Originally Posted by wakatana
I am sorry if i typed something which is un clearly. I have followings files in directory

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

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

Previous Thread | Next Thread
Test Your Knowledge in Computers #698
Difficulty: Medium
"Homesteading the Noosphere" (abbreviated HtN) is an essay written by Eric S. Raymond about the social workings of proprietary, corporate software development.
True or False?

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

Featured Tech Videos