6 Replies - 1341 Views - Last Post: 14 May 2012 - 04:09 AM Rate Topic: -----

#1 rodney_bunkley  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 07-May 12

How do I translate this Korn Shell script to C-Shell?

Posted 13 May 2012 - 08:31 PM

Does anybody know how to translate this Korn Shell script into a C-Shell script??

#!/bin/sh

sum=0

for file in `ls `
do
  if [ -f $file ]
  then
    size=`ls -l $file | awk ' {print $5}'`

    sum=`expr $sum + $size`

  fi

done

echo "$sum"

Is This A Good Question/Topic? 0
  • +

Replies To: How do I translate this Korn Shell script to C-Shell?

#2 no2pencil  Icon User is offline

  • Admiral Fancy Pants
  • member icon

Reputation: 5345
  • View blog
  • Posts: 27,296
  • Joined: 10-May 07

Re: How do I translate this Korn Shell script to C-Shell?

Posted 13 May 2012 - 08:33 PM

I don't see why that wouldn't run under the C-Shell. Your Shebang isn't even shell specific.
Was This Post Helpful? 0
  • +
  • -

#3 sepp2k  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2118
  • View blog
  • Posts: 3,244
  • Joined: 21-June 11

Re: How do I translate this Korn Shell script to C-Shell?

Posted 14 May 2012 - 01:45 AM

View Postno2pencil, on 14 May 2012 - 05:33 AM, said:

I don't see why that wouldn't run under the C-Shell. Your Shebang isn't even shell specific.


The C-Shell isn't Bourne Shell compatible and has quite different syntax. So there's plenty of things in the script that won't run in the C shell.

@OP:

The syntax to set a variable in csh is set VARIABLE = VALUE. The syntax for a for loop is:

foreach VARIABLE in (ITEMS)
    stuff
end



The syntax for an if-statement is:

if(CONDITION) then
    stuff
end


The expression to check for a file is -f FILENAME, just like in test. The syntax to set a variable to the result of an expression is @ VARIABLE = Expression.

That should be everything you need to convert your script to csh.
Was This Post Helpful? 2
  • +
  • -

#4 rodney_bunkley  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 07-May 12

Re: How do I translate this Korn Shell script to C-Shell?

Posted 14 May 2012 - 02:15 AM

View Postsepp2k, on 14 May 2012 - 01:45 AM, said:

View Postno2pencil, on 14 May 2012 - 05:33 AM, said:

I don't see why that wouldn't run under the C-Shell. Your Shebang isn't even shell specific.


The C-Shell isn't Bourne Shell compatible and has quite different syntax. So there's plenty of things in the script that won't run in the C shell.

@OP:

The syntax to set a variable in csh is set VARIABLE = VALUE. The syntax for a for loop is:

foreach VARIABLE in (ITEMS)
    stuff
end



The syntax for an if-statement is:

if(CONDITION) then
    stuff
end


The expression to check for a file is -f FILENAME, just like in test. The syntax to set a variable to the result of an expression is @ VARIABLE = Expression.

That should be everything you need to convert your script to csh.


Thank you sepp2k...that was very helpful. I am truly grateful.
Was This Post Helpful? 0
  • +
  • -

#5 baavgai  Icon User is online

  • Dreaming Coder
  • member icon

Reputation: 5846
  • View blog
  • Posts: 12,703
  • Joined: 16-October 07

Re: How do I translate this Korn Shell script to C-Shell?

Posted 14 May 2012 - 03:53 AM

View Postno2pencil, on 13 May 2012 - 10:33 PM, said:

Your Shebang isn't even shell specific.


WTF? sh IS Bourne shell.

Granted, some Linux flavors will alias it off to Bourne compatible shells, usually bash but now often ash. However, if you really want bash functionality, you should be using bash. The sh is Bourne.

That said, why in the world would anyone willingly use C-Shell... :P
Was This Post Helpful? 2
  • +
  • -

#6 no2pencil  Icon User is offline

  • Admiral Fancy Pants
  • member icon

Reputation: 5345
  • View blog
  • Posts: 27,296
  • Joined: 10-May 07

Re: How do I translate this Korn Shell script to C-Shell?

Posted 14 May 2012 - 03:56 AM

View Postbaavgai, on 14 May 2012 - 06:53 AM, said:

View Postno2pencil, on 13 May 2012 - 10:33 PM, said:

Your Shebang isn't even shell specific.


WTF? sh IS Bourne shell.

My understanding (& please, correct me if I'm wrong), you plug your shell in the Shebang :

/bin/ksh or /bin/korn93 for korn
/bin/bash for bash

However if you just use the shell of the currently logged in session, you use /bin/sh, as it is not specific. So if someone codes a script in korn, & they log in with C-shell, the Shebang of /bin/sh will point to C-shell. Or am I wrong on this?
Was This Post Helpful? 0
  • +
  • -

#7 sepp2k  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2118
  • View blog
  • Posts: 3,244
  • Joined: 21-June 11

Re: How do I translate this Korn Shell script to C-Shell?

Posted 14 May 2012 - 04:09 AM

View Postno2pencil, on 14 May 2012 - 12:56 PM, said:

However if you just use the shell of the currently logged in session, you use /bin/sh, as it is not specific. So if someone codes a script in korn, & they log in with C-shell, the Shebang of /bin/sh will point to C-shell. Or am I wrong on this?


Yes, you are. If the shebang says /bin/sh, the script will be opened with /bin/sh¹ (that is the executable file (or symlink) "sh" that lies in the directory "/bin"). As baavgai said /bin/sh might be a symlink to bash or ash (or any other bourne-compatible shell), or it might be its own executable. Either way that does not depend on the shell with which you're currently logged in.

¹ Assuming it's invoked directly, i.e. `/path/to/the/script` or `./script` (or `script` if the script lies somewhere in the $PATH). If you invoke the script as `sh script` or `bash script` etc., it will be opened with that shell of course.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1