The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
gcd.sh script doesn't work... kantze Shell Programming and Scripting 1 01-17-2008 09:46 PM
Modify Perl script to work with txt - Permissions script joangopan Shell Programming and Scripting 1 09-13-2007 12:38 AM
My script does not work - could you pls help? BearCheese Shell Programming and Scripting 1 06-29-2007 06:12 AM
Script doesn't work, but commands inside work cheongww UNIX for Dummies Questions & Answers 2 11-14-2006 10:52 PM
sed script. How does it work? billy5 Shell Programming and Scripting 2 09-02-2005 05:45 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 10-14-2008
cfajohnson's Avatar
cfajohnson cfajohnson is offline Forum Advisor  
Shell programmer, author
  
 

Join Date: Mar 2007
Location: Toronto, Canada
Posts: 2,365

Read the comments I posted earlier.

Answer the questions I posed.

Why are you using:


Code:
for i in `ls -1 | grep $1 | grep  $2`

That is almost certainly the wrong way to do whatever you are trying to do.

Where are you compiling a list? All I see is the display of the individual commands, and nowhere do you put them into a list.
  #2 (permalink)  
Old 10-14-2008
llsmr777 llsmr777 is offline
Registered User
  
 

Join Date: May 2007
Posts: 58
I put the done after I get my listing instead of the end of the script.

Can I put my mv statement inside my Case Statement?

You asked me "how are you using it? We're not mind readers" to my comment "I am using it to ask the user if the list is correct"
Not sure what you are asking me?
I want my case statement to ask the user if the list (mv commands plus name of files) outputed to the screen is the files that they want renamed and then moved.
I hope that answers that questions correctly.

And yes I meant that the case does carry out what is to happen if the user says yes or no.
Yes- rename file from .Sent to .Done and then move to a subdirectly called DONE
No - just output to the screen "no changes made..."

I did change the for string to what you suggested.

I will change:
x=`echo $i | sed 's/\.Sent/\.Done/g'`
TO:
x=${i%%.Sent*}.Done${i#*.Sent}
But can you explain this syntax to me? I want to understand it before I just change it.

AGain I appreciate all your help and patience.
  #3 (permalink)  
Old 10-14-2008
cfajohnson's Avatar
cfajohnson cfajohnson is offline Forum Advisor  
Shell programmer, author
  
 

Join Date: Mar 2007
Location: Toronto, Canada
Posts: 2,365
Quote:
Originally Posted by llsmr777 View Post
I put the done after I get my listing instead of the end of the script.

Can I put my mv statement inside my Case Statement?

Of course.

Make sure the code you use is correct; in the script you posted, it was incomplete.
Quote:
You asked me "how are you using it? We're not mind readers" to my comment "I am using it to ask the user if the list is correct"
Not sure what you are asking me?

Just saying you made a change is not enough; I need to see the code you are trying to execute.
Quote:

I want my case statement to ask the user if the list (mv commands plus name of files) outputed to the screen is the files that they want renamed and then moved.
I hope that answers that questions correctly.

And yes I meant that the case does carry out what is to happen if the user says yes or no.

The case statment does not carry out the action; it makes a decision about which action is to be carried out. You then need the code to carry out the action.

You might find it helpful to put sections of your code into functions so that they can be tested idependently.

For example, you might want these functions: build_list, show_list, ask_user and move_files.

After you have written and tested the functions, the rest of your script could look like this:


Code:
build_list <file pattern>
show_list
if ask_user
then
   move_files
else
   echo "No changes made ..."
fi

Quote:

Yes- rename file from .Sent to .Done and then move to a subdirectly called DONE
No - just output to the screen "no changes made..."

I did change the for string to what you suggested.

I didn't suggest anything. I posted an example of what might be a better way of doing it. I don't know whether it is correct or not because you haven't explained the file selection criteria.

Does it do what you want? Does the wildcard pattern match the files you want?

What values are $1 and $2 likely to contain?

Does the following code display the files you want to work with?


Code:
printf "%s\n" *$1*$2*

Quote:
I will change:
x=`echo $i | sed 's/\.Sent/\.Done/g'`
TO:
x=${i%%.Sent*}.Done${i#*.Sent}
But can you explain this syntax to me? I want to understand it before I just change it.

It is explained in the "parameter expansion" section of your shell's man page. To see what the expansion does, try printing the individual parts:


Code:
printf "%s\n" "$i" "${i%%.Sent*}" "${i#*.Sent}"

  #4 (permalink)  
Old 10-21-2008
ghostdog74 ghostdog74 is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2006
Posts: 2,555
Quote:
Originally Posted by llsmr777 View Post
I will change:
x=`echo $i | sed 's/\.Sent/\.Done/g'`
TO:
x=${i%%.Sent*}.Done${i#*.Sent}
you don't have to change if you think the sed version is more understandable to you.
  #5 (permalink)  
Old 10-21-2008
llsmr777 llsmr777 is offline
Registered User
  
 

Join Date: May 2007
Posts: 58
Ok here is what I changed it to. I haven't tested it because I wanted input first.


Code:
function build_list
{
for i in *$1*$2*
do
x=`echo $i | sed 's/\.Sent/\.Done/g'`
done
}

function show_list
{
echo mv $i DONE/$x
}

echo "Is this Ok?"
read user_response
if [$user_response="y"|"Y"]
then
 mv $i DONE/$x;;
else
if [$user_response="n"|"N"]
then
echo "No changes made ...";;
fi
fi

  #6 (permalink)  
Old 10-21-2008
sethcoop sethcoop is offline
Registered User
  
 

Join Date: Oct 2008
Location: United States
Posts: 34
You need to have a loop that prints the list then waits for the input of "Y|y"... if it is yes then have another loop that actually moves the file...

I would have a loop that prints the files and if it is "yes" then in the if statement have another loop that moves the files... (same loop but remove the "echo")...
  #7 (permalink)  
Old 10-21-2008
cfajohnson's Avatar
cfajohnson cfajohnson is offline Forum Advisor  
Shell programmer, author
  
 

Join Date: Mar 2007
Location: Toronto, Canada
Posts: 2,365
Quote:
Originally Posted by llsmr777 View Post
Ok here is what I changed it to. I haven't tested it because I wanted input first.

If you had tested it first, you would have found most of the errors.

If you cannot tell us how it is not working, how do you expect us to help?
Quote:


Code:
function build_list

That is not wrong in ksh and bash, but it is not the standard syntax for defining a function and will not work in other shells. Standard is:


Code:
build_list()

Quote:

Code:
{
for i in *$1*$2*
do
x=`echo $i | sed 's/\.Sent/\.Done/g'`
done
}

Tha function does not build a list; only the last filename that matched the pattern would be stored in $x.
Quote:

Code:
function show_list
{
echo mv $i DONE/$x
}

It makes your script more undertandable if you use meaningful names; that function doesn't show a list, it moves files.

...and it will fail if any filenames contain spaces or other pathological characters.

..and there is no logic behind that code. Think about what you are doing.
Quote:

Code:
echo "Is this Ok?"
read user_response
if [$user_response="y"|"Y"]

There are five errors in the line above.
Quote:

Code:
then
 mv $i DONE/$x;;
else
if [$user_response="n"|"N"]
then
echo "No changes made ...";;
fi
fi
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 03:14 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0