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"
|
Quote:
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. |
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 |
Quote:
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. |
Code:
#!/bin/bash |
Quote:
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. |
Quote:
Code:
#!/bin/tcsh Code:
#!/bin/csh |
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. |