Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ksh for each file in a directory 1

Status
Not open for further replies.

awood69

Programmer
Feb 27, 2012
19
US
Newbie to ksh any help would be appreciated.

I need to loop through a directory and copy all the *.xls files to a different directory based off of what value is returned by a sql query. Something like this...


for each file in folder
where file like *.xls

run sql

cp to new folder
 
Hi

That translated to Ksh would look like :
Code:
[b]for[/b] file [b]in[/b] folder[teal]/*.[/teal]xls[teal];[/teal] [b]do[/b]
  [navy]result[/navy][teal]=[/teal][green][i]"$( run sql )"[/i][/green]
  [b]if[/b] [teal][[[/teal] [green][i]"$result"[/i][/green] [teal]==[/teal] [green][i]'whatever'[/i][/green] [teal]]];[/teal] [b]then[/b]
    cp [green][i]"$file"[/i][/green] [green][i]'new folder'[/i][/green]
  [b]fi[/b]
[b]done[/b]

[gray]# or supposing the SQL query returns the new directory's name :[/gray]

[b]for[/b] file [b]in[/b] folder[teal]/*.[/teal]xls[teal];[/teal] [b]do[/b]
  [navy]destination[/navy][teal]=[/teal][green][i]"$( run sql )"[/i][/green]
  cp [green][i]"$file"[/i][/green] [green][i]"$destination"[/i][/green]
[b]done[/b]

Feherke.
 
Since you're new to the Korn Shell then this may help you with comparisons.

From the kornshell.com faq:
Q11. How come [[ $foo == $bar ]] is true and [[ $bar == $foo ]] is false?
A11. The == operator is not symmetrical. It takes a string on the left
and a pattern on the right. However, if you double quote the right
hand side, which removes the special meaning of pattern match
characters, then this becomes a string comparison so that
[[ "$foo" == "bar" ]] and [[ "$bar" == "$foo" ]] are equivalent.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top