Help can't get script to work how I need it to...


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help can't get script to work how I need it to...
# 8  
Old 10-14-2008

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.
# 9  
Old 10-14-2008
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.
# 10  
Old 10-14-2008
Quote:
Originally Posted by llsmr777
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}"

# 11  
Old 10-21-2008
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

# 12  
Old 10-21-2008
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")...
# 13  
Old 10-21-2008
Quote:
Originally Posted by llsmr777
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

# 14  
Old 10-21-2008
Quote:
Originally Posted by llsmr777
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. IP Networking

Discussion at work, would a router work pluging a cable in wan1 and lan1?

hi all. and sorry for the random question, but this sparkled a raging flame-war at work and i want more points of view situation a router, with linux of some sort, dhcp client requesting for ip in wan1 (as usual with wan ports) dhcp server listening in lan1, and assigning ip (as usual... (9 Replies)
Discussion started by: broli
9 Replies

2. Shell Programming and Scripting

My script work on Linux but not work in sunos.

My script work on Linux but not work in sun os. my script. logFiles="sentLog1.log sentLog2.log" intial_time="0 0" logLocation="/usr/local/tomcat/logs/" sleepTime=600 failMessage=":: $(tput bold)Log not update$(tput rmso) = " successMessage="OK" arr=($logFiles)... (7 Replies)
Discussion started by: ooilinlove
7 Replies

3. UNIX for Dummies Questions & Answers

this cshell script does not work ??

Hi I am trying to put the following commands that i have to type manually at the cshell prompt into a cshell script startup.csh which is copied below echo $DISPLAY xhost + rsh ba08lo01 module load incisiv/102/10.20.035 setenv DISPLAY $DISPLAY When i run the script with source command... (2 Replies)
Discussion started by: kaaliakahn
2 Replies

4. Shell Programming and Scripting

my script doesn't work :(

i have this script and when i ejecute it, the console tell me this " sintax error line 41 unexpected element "}" " is the sintaxis ok? #!/bin/bash if ;then { exit 0; } if ; then { sudo /etc/init.d/apache2 start; sudo /etc/init.d/mysql start; php5 & nautilus... (3 Replies)
Discussion started by: keiserx
3 Replies

5. UNIX for Dummies Questions & Answers

Script syntax...does this work?

Hi all, I'm just a basic unix scripto...does the following lines work?? I saw this lines from a script I was about to modify, please take note that these are not the original directories and the sign ( " ) is in there. "/destination/directory" "/origin/directory" If I add a command line in... (3 Replies)
Discussion started by: 3rr0r_3rr0r
3 Replies

6. Shell Programming and Scripting

Modify Perl script to work with txt - Permissions script

Hi I have this code, and i want work with a ls -shalR output in .txt What i need read to do this?? Where start? #!/usr/bin/perl # Allrights- A perl tool for making backups of file permissions # Copyright (C) 2005 Norbert Klein <norbert@acodedb.com> # This program is free... (1 Reply)
Discussion started by: joangopan
1 Replies

7. Shell Programming and Scripting

My script does not work - could you pls help?

Hi all, I put together a script that seems not working as I would like to and after spending hours to find the problem I decided to ask your help. The thing I am trying to do is call AWK command on the TRUE branch of if statement. Else branch should only print out a message to screen. This... (1 Reply)
Discussion started by: BearCheese
1 Replies

8. Shell Programming and Scripting

Why does this script not work? Please help

I have created a Shell script to call 12 Oracle PL/SQL scripts to query some data and insert into a table. When I run shell scripts manually, it works very well. But when I schedule shell script in crontab, it will give the error message like this: 02/22/07 04:00:00: Database mydb.world is up... (4 Replies)
Discussion started by: duke0001
4 Replies

9. UNIX for Dummies Questions & Answers

Script doesn't work, but commands inside work

Howdie everyone... I have a shell script RemoveFiles.sh Inside this file, it only has two commands as below: rm -f ../../reportToday/temp/* rm -f ../../report/* My problem is that when i execute this script, nothing happened. Files remained unremoved. I don't see any error message as it... (2 Replies)
Discussion started by: cheongww
2 Replies

10. UNIX for Dummies Questions & Answers

Command work but not in SH script

Command works but not in SH At terminal if i type: scp test.tar.gz user1@server2:/home/user Everything run smoothly (keyed, no password need) At script , test.sh #!/bin/sh scp test.tar.gz user1@server2:/home/user Nothing happen and clue ? ? ? ? ? (3 Replies)
Discussion started by: cititester
3 Replies
Login or Register to Ask a Question