0

I am trying to adapt the loop code from bash to csh:

 while read file; do

   if ! UPLOAD_FILE=$(ls "$UPLOAD_ARCHIVE_DIR"/*$file* 2>/dev/null); then
     echo "could not find $file"
     (( MISSING_COUNTER++ ))
     continue
   fi

   ls -l "$UPLOAD_FILE" | sed 's/^[^ ]* *[^ ]* *[^ ]* *[^ ]* *[^ ]* *//'
   cp "$UPLOAD_FILE" "$PROCESS_FILES_DIR"
   DOS_FILE=$(basename "$UPLOAD_FILE")
   SOR_FILE=$(echo $DOS_FILE | sed 's/.dat/.sor/')
   unix2dos "$PROCESS_FILES_DIR"/"$DOS_FILE" 1>/dev/null 2>&1
   mv "$PROCESS_FILES_DIR"/"$DOS_FILE" "$UPLOAD_PROCESS_DIR"

   (( FOUND_COUNTER++ ))
 done < "$1"

The input information comes from variable $input and contains a list, here is a sample record from the list: PL000000002002638908

So far, I have tried to use foreach and while loops without success, any help is greatly appreciate it!

sayayinx
  • 21
  • 4
  • 4
    Why are you doing this? bash is generally considered to be a much better scripting language. (I'm not sure how well a `while read ...` loop can be emulated in csh.) See, for example, http://www.perl.com/doc/FMTEYEWTK/versus/csh.whynot. – Keith Thompson Jan 16 '14 at 18:31
  • 1
    This question appears to be off-topic because it is too broad. The question should be edited to ask specific questions about particular parts you're having trouble translating, rather than asking broadly about a large block of code. – John Kugelman Jan 16 '14 at 18:39
  • @keith-thompson The reason why I need to use csh is because it is the only environment on the server and they have denied me using bash. – sayayinx Jan 16 '14 at 18:58
  • @john-kugelman I have specified the topic in question. Also, I have removed extra information from the code. – sayayinx Jan 16 '14 at 19:01
  • You are saying that `/bin/sh` on the machine is actually `csh`? I find that unlikely, and it would be much easier to make the `bash` code shown POSIX-conformant than to translate it to `csh`. – chepner Jan 16 '14 at 19:08
  • @chepner thanks, trust me when I tell you that the code for bash works perfect and this odd request is something unavoidable. It is pretty much csh or nothing :( – sayayinx Jan 16 '14 at 20:00
  • I'm distinguishing between `bash` and `sh`; I'm unaware of any Unix system that uses `csh` as the default system shell. – chepner Jan 16 '14 at 20:07
  • @chepner: There are BSDish systems that use `csh` as the default *interactive* shell (by setting `/bin/csh` as the shell in `/etc/passwd`), but `/bin/sh` should still be a Bourne-like shell. – Keith Thompson Jan 16 '14 at 20:14
  • Every unix has a Bourne-style shell as `sh`, almost always located in `/bin/sh`. It may be something more restricted than bash, but usually if bash isn't available then `ksh` is, and your script would work in either. – Gilles 'SO- stop being evil' Jan 16 '14 at 20:15

1 Answers1

1

You almost certainly don't need to translate your script from bash to csh.

/bin/sh is the Bourne shell, or something that works very much like it. This should be the case even on systems where csh is the default interactive shell and bash is unavailable.(On some systems, /bin/sh it may be a symlink to ksh, for example.)

bash is derived from the Bourne shell, and adds some bash-specific features.

You can confirm that /bin/sh is a Bourne-like shell by typing the following command at any shell prompt:

/bin/sh -c 'if true ; then echo Yes, this is a Bourne-like shell ; fi'

In the unlikely event that /bin/sh is really csh, the above will give you a syntax error.

It would be easier to modify your script to avoid bash-specific features than to translate it to csh. Read the bash manual, look for Bash-specific features, and modify your script to avoid them. (If that's even necessary; it may be that /bin/sh is something that supports at least some of them.)

Change the first line of your script to

#!/bin/sh

and see what error messages you get.

You might need to replace the

$(command)

syntax by the older

`command`

and this:

(( FOUND_COUNTER++ ))

to something using expr, perhaps:

FOUND_COUNTER=`expr $FOUND_COUNTER + 1`

The while read file ... loop should work as it is.

Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
  • thank you, unfortunately this arcaic system is supposed to run csh, anything else is not allowed. – sayayinx Jan 16 '14 at 19:59
  • It's *very* likely that `/bin/sh` is a Bourne-like shell. The Bourne shell is *older* than csh. I've updated my answer to show how to confirm or disprove this, starting at the 4th paragraph. If `/bin/sh` really isn't a Bourne-like shell, what OS is this system running? – Keith Thompson Jan 16 '14 at 20:13
  • `$(...)` is supported by `/bin/sh`, backquotes will not be necessary. `FOUND_COUNTER=$(( $FOUND_COUNTER + 1 ))` is also supported by `/bin/sh`. – chepner Jan 16 '14 at 22:27
  • @chepner: It depends on the system. The original Bourne shell did not support either of those features. The OP says the system he's using is "archaic", so it's likely that `/bin/sh` is an older Bourne-like shell. For example, I'm currently logged into a Solaris 9 system on which `/bin/sh` reports syntax errors for both of those features. But it's certainly worth checking (which is why I said the OP *might* need to replace those constructs); if `/bin/sh` happens to be a bit more modern, that will save some work. – Keith Thompson Jan 16 '14 at 22:34