LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   running sh scripts from csh command line (http://www.linuxquestions.org/questions/linux-newbie-8/running-sh-scripts-from-csh-command-line-803266/)

casperdaghost 04-21-2010 07:53 AM

running sh scripts from csh command line
 
i use the csh shell at work. you can either log on to servers locally or to the NFS mount. is is important to know which you are mounted to when executing scripts. csh is the only shell that lets me know which high availability server i am currently on.

before i write a shell script to complete a task, i have to type sh or bash at the prompt, hit enter and then configure the script. then when i am done, hit control -d to get out the bash or sh shell.

is there a way to run sh or bash scripts from the csh command line - i have tried typing this at the csh command line: /bin/sh (or ./bin/bash)
while [ 1 ]; do ps auxwww | grep fire | wc -l ; done ... etc, but it does not work.

we do not have the zsh shell at work

linuxlover.chaitanya 04-21-2010 08:13 AM

From the c shell cd to the directory where the shell script is and then run the script like

sh script.sh.

tronayne 04-21-2010 08:25 AM

Try this: as the first line of your shell program, enter
Code:

#!/bin/sh
or
#!/bin/ksh
or
#!/bin/bash

That'll execute in the appropriate shell.

Also, particularly with C-Shell, you can set environment variables in your shell program that may not be set in C-Shell; e.g., simply
Code:

#!/bin/sh

export ENV_VAR=blah
export OTHER_ENV_VAR=blahblah

Life get better that way.

Hope this helps some.

casperdaghost 04-21-2010 10:25 AM

so i am not at work right now, but you are explaining that i should be able to go

cd /bin/sh ; while [ 1 ] ; do ps auxwww | grep fire | wc -l ; done

right from the csh command line - that would be acceptable.

tronayne 04-21-2010 11:11 AM

I'm sorry, I don't quite get what you're asking -- first off, you would not "cd /bin/sh" (/bin/sh is an executable file, not a directory). Many installations do not have the current directory in the user's PATH environment, so, to execute a shell program ("script"), you can cd to the directory where it resides then enter "./shell_prog_name" to execute that program. If your shell program has the "#!/bin/sh" as the first line of the program, it will be executed by the appropriate shell.

You can normally execute any executable program (irrespective of what the source code may be) from the C-Shell command prompt; the example you've given is a Shell while loop (which will not, I'm sure you've discovered, execute in C-Shell). You can,
Code:

% /bin/sh                    (this enters the Bourne Shell from the C-Shell)
# while ...                  (your while)
^D                          (exit the Bourne shell; back to the C-Shell)
%

and that will work.

catkin 04-21-2010 11:34 AM

Quote:

Originally Posted by casperdaghost (Post 3942753)
so i am not at work right now, but you are explaining that i should be able to go

cd /bin/sh ; while [ 1 ] ; do ps auxwww | grep fire | wc -l ; done

right from the csh command line - that would be acceptable.

No -- the technique given was to run a bash script, not to execute bash commands. If you want to be able to execute bash commands like that you need to do as you wrote "before i write a shell script to complete a task, i have to type sh or bash at the prompt, hit enter and then configure the script. then when i am done, hit control -d to get out the bash or sh shell". But there may be some misunderstanding; "writing a shell script" is creating a file with shell commands in it and then running the file as a script, thus running the commands in it. When you write "configure the script", do you mean entering commands at the prompt or creating a file with shell commands in it?

casperdaghost 04-22-2010 06:12 AM

i mean entering commands at the prompt.

yeah, please don't work to hard on this. just figured i would give it a shot.

alot of times when i am running a job that does not exit cleanly, i will log in to another shell and type this in directly into the command line -
while [ 1 ] do ; ps auxwww | grep somejobUncleanExit | wc -l ; date; sleep 5; done
this will return a one or a zero - which lets me know, within 5 seconds when the job is done.

i can't run this from the csh shell - i have to type in "sh" first then hit <enter>, then type in the command.

so this is not a text file that I run ./fromTheDirectory, it is just something that i type into the command line.

i do this at least 40 times a night. i don't have permissions to save anything to many of these drives.

typing the sh, switching to the bash shell is not a big hassle - I was just wondering if there was a way i could do this from the csh shell.

sure i cold just use the sh shell - but for some reason the bourne shell 'pwd' does not distinguish from a local or an NFS mounted drive. the csh shell does - and running a NFS job on a local machine is bad. so what i have is a bunch of sh shells open with the csh paths as xterm --title's, spitting out 1's or 0's. it is cluttered, but i guess that it is going to have to due, because there does not seem to be any way to type in and execute a sh command at the csh prompt.


All times are GMT -5. The time now is 10:45 PM.