foreach loop using a "*" for args


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting foreach loop using a "*" for args
# 1  
Old 03-19-2008
Bug foreach loop using a "*" for args

have a script that copies a file to a bunch of hosts. works great with a single file. How do I make it copy all files in a source directory? or a *txt pattern, or anything using a * ???

name of script: cp2all
#!/bin/csh
foreach host ( host1 host2 host3 host4 host5 host6 host7)
rcp $argv[1] $host\:$argv[2]
end


i usually run the script to copy a single file like this
./cp2all test1.txt /home/ajp
this will copy test1.txt to /home/ajp on all the hosts listed in foreach.

I want to copy all files in a directory, but using * doesnt seem to work. How would I do this??

Thank you as always!!
# 2  
Old 03-19-2008
Hi.

One way:
Code:
#!/usr/bin/env tcsh

# @(#) s1       Demonstrate foreach processing of filename list.

echo
echo "(Versions displayed with local utility version)"
sh -c "version >/dev/null 2>&1" && version =o tcsh
echo

touch t1 t2 t3
set files = ( `ls -1` )

foreach file ( $files )
  echo " variable file is $file"
end

Producing:
Code:
% ./s1

(Versions displayed with local utility version)
Linux 2.6.11-x1
tcsh 6.13.00

 variable file is s1
 variable file is t1
 variable file is t2
 variable file is t3

Best wishes ... cheers, drl

Standard advice: avoid csh family shells for scripting, use Bourne family shells instead.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash script - Print an ascii file using specific font "Latin Modern Mono 12" "regular" "9"

Hello. System : opensuse leap 42.3 I have a bash script that build a text file. I would like the last command doing : print_cmd -o page-left=43 -o page-right=22 -o page-top=28 -o page-bottom=43 -o font=LatinModernMono12:regular:9 some_file.txt where : print_cmd ::= some printing... (1 Reply)
Discussion started by: jcdole
1 Replies

2. Shell Programming and Scripting

Failure: if grep "$Var" "$line" inside while read line loop

Hi everybody, I am new at Unix/Bourne shell scripting and with my youngest experiences, I will not become very old with it :o My code: #!/bin/sh set -e set -u export IFS= optl="Optl" LOCSTORCLI="/opt/lsi/storcli/storcli" ($LOCSTORCLI /c0 /vall show | grep RAID | cut -d " "... (5 Replies)
Discussion started by: Subsonic66
5 Replies

3. UNIX for Dummies Questions & Answers

Using "mailx" command to read "to" and "cc" email addreses from input file

How to use "mailx" command to do e-mail reading the input file containing email address, where column 1 has name and column 2 containing “To” e-mail address and column 3 contains “cc” e-mail address to include with same email. Sample input file, email.txt Below is an sample code where... (2 Replies)
Discussion started by: asjaiswal
2 Replies

4. Shell Programming and Scripting

Question about the difference between "for" and "while" loop

dear all, i got some questions about for/while loop when working on redirect command output to for/while loop. take one for example : in for loop : allfiles=`find /var/log -maxdepth 1 -type f -mtime +5` index=1 for ((i=0; i<${#allfiles}; i++)); do echo "$index:${allfiles}" ... (2 Replies)
Discussion started by: tiger2000
2 Replies

5. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

6. UNIX for Dummies Questions & Answers

BASH: Interactive "cp" (and "mv") in a loop

Hi. I have a copy-file script (and a move-file script) that I recently tried to make interactive. I tested the former on three files from a text list, and watched to see what would happen. As the cp command was in a while/do/done loop, there was no pause for input: it wrote the file from the... (2 Replies)
Discussion started by: SilversleevesX
2 Replies

7. Shell Programming and Scripting

Perl Foreach add "4" to the end of each element

I wrote the following script configuring cisco router:#!/usr/bin/perl use strict; use warnings; use Getopt::Long; use Opsware::NAS::Connect; my @IP = split(/\./,"$tc_device_ip$"); my $O3 = $IP; my($host, $port, $user, $pass) =... (3 Replies)
Discussion started by: ahmed_zaher
3 Replies

8. UNIX for Dummies Questions & Answers

Explain the line "mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'`"

Hi Friends, Can any of you explain me about the below line of code? mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'` Im not able to understand, what exactly it is doing :confused: Any help would be useful for me. Lokesha (4 Replies)
Discussion started by: Lokesha
4 Replies

9. Shell Programming and Scripting

getopts and "priority level" for args

Hi, I use getopts in this way:while getopts ":d:f:crapv" Option do case $Option in d ) BACKUP_DIR="$OPTARG";echo $BACKUP_DIR;; #echo fot test c ) compress_file;; r ) remove_file;; a ) remove_file && compress_file;;... (2 Replies)
Discussion started by: mbarberis
2 Replies
Login or Register to Ask a Question