LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 02-17-2009, 05:58 AM   #1
wolsch
LQ Newbie
 
Registered: Feb 2009
Posts: 2

Rep: Reputation: 0
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"
 
Old 02-17-2009, 06:24 AM   #2
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,063

Rep: Reputation: 381Reputation: 381Reputation: 381Reputation: 381
Quote:
Originally Posted by wolsch View Post
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.
 
Old 02-17-2009, 06:28 AM   #3
wolsch
LQ Newbie
 
Registered: Feb 2009
Posts: 2

Original Poster
Rep: Reputation: 0
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
 
Old 02-17-2009, 06:38 AM   #4
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,063

Rep: Reputation: 381Reputation: 381Reputation: 381Reputation: 381
Quote:
Originally Posted by wolsch View Post
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.
 
Old 02-17-2009, 06:45 AM   #5
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
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##*/}.
 
Old 02-17-2009, 07:01 AM   #6
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,063

Rep: Reputation: 381Reputation: 381Reputation: 381Reputation: 381
Quote:
Originally Posted by colucix View Post
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.

Last edited by i92guboj; 02-17-2009 at 07:03 AM.
 
Old 02-17-2009, 08:18 AM   #7
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
Quote:
Originally Posted by i92guboj View Post
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
 
Old 02-17-2009, 10:14 AM   #8
bach-fiend
LQ Newbie
 
Registered: Feb 2009
Location: St Paul, Minnesota, USA
Distribution: Redhat Fedora 10; someday planet CCRMA
Posts: 11

Rep: Reputation: 0
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Can I use 'if' statements in Expect script? d3funct Programming 3 02-24-2011 04:59 PM
Case statements and vars for bash script geech Programming 1 01-09-2009 05:49 PM
Proper Syntax for If...Then..Else statements with a script kaplan71 Linux - Software 2 11-11-2008 01:44 PM
shell script having multiple grep statements-I want input file to be read only once mukta9003 Linux - Newbie 4 08-27-2008 01:58 AM
if statements and case statements not working in bourne shell script mparkhurs Programming 3 06-12-2004 03:41 AM


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

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration