Aviad
Aviad

Reputation: 3584

TCSH/CSH | assign variable with commands result

I wrote this code :

      1 #!/bin/tcsh
      2
      3 set myFiles  = `ls`
      4 set i = 1;
      5 echo "argc is $#argv"
      6 while ($i <= $#argv)
      7    $myFiles = `echo $myFiles | tr "$argv[$i]" " "`
      8    echo "argv now is $argv[$i]"
      9    echo "my files are : $myFiles"
     10    @ i++;
     11 end
     12 echo "my files post proccess are $myFiles"
     13 foreach name ($myFiles)
     14    set temp = `cat $name`
     15    echo "temp is : $temp"
     16    unset temp
     17 end

This piece should get a list of file names within the current folder, and print the content of the files that are not specified IE : folder has the files : A B C D E and the input is : A B C so the content of D E will be printed.

now the logic is right, but I have some syntactic issues regarding line 7 (the tr) I've tried with sed as well, but I get "permission denied" to the console for some reason, and I really don't know how to fix it.

So the help I need is actually syntactic regarding assigning a variable with commands output plus including other variables within those commands.

Hope that's alright..

THANKS !

Upvotes: 1

Views: 2640

Answers (1)

Victor Burenkov
Victor Burenkov

Reputation: 187

Please note that tr will replace all matching characters, so if your input includes "A", it will replace all "A" with " " in all file names returned by ls.

There is a much cleaner solution. You want to find all files, exclude files matching the input and print what is left. Here you go:

#!/bin/tcsh

set exclude_names = ""

# if any argument is passed in, add it as "! -name $arg"
foreach arg ( $* )
    set exclude_names = "$exclude_names ! -name $arg"
end

# Find all files in the current dir, excluding the input
# then print out the filtered set of files
find . -maxdepth 1 -type f $exclude_names -exec cat {} +

Upvotes: 1

Related Questions