LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   if then statements, determining a script file (https://www.linuxquestions.org/questions/linux-newbie-8/if-then-statements-determining-a-script-file-705309/)

wolsch 02-17-2009 04:58 AM

if then statements, determining a script file
 
I am writing a script file that will classify its inputs. How would i check the first line of a file to see if it contains "#!/bin/tcsh" or "#! /bin/tcsh" and if the first line does contain one of these strings the if statement should just return the words "tcsh script"

i92guboj 02-17-2009 05:24 AM

Quote:

Originally Posted by wolsch (Post 3446692)
I am writing a script file that will classify its inputs. How would i check the first line of a file to see if it contains "#!/bin/tcsh" or "#! /bin/tcsh" and if the first line does contain one of these strings the if statement should just return the words "tcsh script"

Mmm, I am not completely sure I understood you. Do you want to check the first line of the script itself or the first line or another given file?

There are many solutions that you could use in either case. If you really want to check that directly you could use sed, grep and many other creative solutions depending on how much fuzziness is permitted and how many strange cases you hope to find.

If you just want to check the file type of a given file, I suggest using the command "file" instead, which is designed for that purpose.

wolsch 02-17-2009 05:28 AM

i want to check another file that is input into my script file

i am currently trying to use sed and grep but cannot get the return i want

here is my current if then

if (sed "2,9999999d" $argv[$c] | grep "/bin/tcsh" | wc -l) ==1 then
echo tcsh script
endif

i92guboj 02-17-2009 05:38 AM

Quote:

Originally Posted by wolsch (Post 3446724)
i want to check another file that is input into my script file

i am currently trying to use sed and grep but cannot get the return i want

here is my current if then

if (sed "2,9999999d" $argv[$c] | grep "/bin/tcsh" | wc -l) ==1 then
echo tcsh script
endif

Is this script itself written in tcsh/csh?

If affirmative, then I don't know it proficiently so I will not advice you about csh questions specifically (I would probably be misleading you since my knowledge is way too limited).

But I still can advice you about a couple of things. The easiest and probably most portable way to write the check (one of them, anyway) could be with head + grep.

head -n1 will give you the first line, and |grep ^'#!/bin/tcsh' will do the rest of the job.

colucix 02-17-2009 05:45 AM

Code:

#!/bin/bash
sha_bang=$(head -1 $1)
echo ${sha_bang##*/} script

I don't understand the need of an if/then construct. Whatever the sha-bang is, you can extract just the name of the shell using parameter substitution and echo the resulting string. If you need to perform some actions depending on which shell it is, just add a case construct to check the value of ${sha_bang##*/}.

i92guboj 02-17-2009 06:01 AM

Quote:

Originally Posted by colucix (Post 3446744)
Code:

#!/bin/bash
sha_bang=$(head -1 $1)
echo ${sha_bang##*/} script

I don't understand the need of an if/then construct. Whatever the sha-bang is, you can extract just the name of the shell using parameter substitution and echo the resulting string. If you need to perform some actions depending on which shell it is, just add a case construct to check the value of ${sha_bang##*/}.

That's true, but that syntax is bashism, which will not work on other shells. It won't work even in sh compatible shells, other than bash, so let alone tcsh which is only compatible with itself.

I say that because what he's writing is not bash, I guess it's tcsh but as said, I am not proficient with it.

That expression can easily be translated into a more portable sed based solution though.

colucix 02-17-2009 07:18 AM

Quote:

Originally Posted by i92guboj (Post 3446755)
That's true, but that syntax is bashism, which will not work on other shells. It won't work even in sh compatible shells, other than bash, so let alone tcsh which is only compatible with itself.

Uh sorry, I was too focused on bash to notice the C-shell syntax. I think the problem in the script suggested by the OP is the syntax of command substitution. Putting back slashes instead of parentheses, should do the trick:
Code:

#!/bin/tcsh
set c = 1
if `sed "2,9999999d" $argv[$c] | grep "/bin/tcsh" | wc -l` == 1 then
  echo tcsh script
endif

If the problem is to retrieve the shell name, whatever it be, then you can just use sed, as suggested by i92guboj:
Code:

#!/bin/csh
set c = 1
set shell = `head -1 $argv[$c] | sed 's/.*\/\(.*\)$/\1/'`
echo $shell script


bach-fiend 02-17-2009 09:14 AM

You might consider a non-shell solution using a modern scripting language like python or perl. [Or as suggested, use the "file" command.]
Such languages have easier syntax than the shells, and Perl has arguably the best regular expression [pattern matching] around. Python's is also supposed to be very good, but I have very little python experience.

For writing shell scripts, you might use only the original sh [bourne shell] features. Newer shells like Korn and Bash ["Bourne Again SHell"] have nice syntax conveniences, but not all shells support them. As for the C-Shell, it has some nice things for interactive use [so does Bash] but they turn into problems when scripting [Tom Christiansen's "C Shell Programming Considered Harmful" explains why.]

So I only use shell scripting for easy problems and where there is compelling reason to script the shell. I usually just use Perl.


All times are GMT -5. The time now is 07:38 AM.