![]() |
![]() |
#1 |
Aug 2006
598710 Posts |
![]()
I decided to teach myself shell scripting today*, but I got stuck writing my first script. The trouble spot: I don't know how to escape literals in quotes.
If I wanted to test if the first parameter was "dog", I could do this: Code:
if [ "$1" = "dog" ] then echo It's a dog! else echo I have no idea what this is. fi Here's the actual code, though it doesn't work properly: Code:
head4=`head -c4 "$file"` png="\\211PNG" if [ "$head4" = "$png" ] then gpicview "$file" else echo File detection failed. Initial part: $head4 echo Expected: ........................... $png fi |
![]() |
![]() |
![]() |
#2 |
"Nancy"
Aug 2002
Alexandria
2,467 Posts |
![]()
The bash man page says you can create special characters with
$'string' e.g, PNG=$'\x89PNG' Code:
PNG=$'\x89PNG' head4=`head -c4 "fry.png"` if [ "$head4" == "$PNG" ]; then echo 'Good news, everyone!'; fi Good news, everyone! PNG=`echo -e "\0211PNG"` may be more portable. Or is -e a GNU extension again? Using printf (the command line program) instead may be more portable still. Alex |
![]() |
![]() |
![]() |
#3 |
Aug 2006
598710 Posts |
![]() |
![]() |
![]() |
![]() |
#4 |
Aug 2006
5,987 Posts |
![]()
OK, here's another question. To avoid redundancy I'd like to collect the handling for each type of file together (whether detected by extension, header, or some other method). In a batch file I'd just do
Code:
REM There are actually lots of ways of writing this in batch... IF "%1"=="help" GOTO Help . . . :Help . . . GOTO end . . . :end Code:
function Help { . . . } . . . if [ "$1" = "help" ]; then Help . . . Last fiddled with by CRGreathouse on 2009-03-23 at 21:58 |
![]() |
![]() |
![]() |
#5 |
"Nancy"
Aug 2002
Alexandria
2,467 Posts |
![]()
I'm not sure I understand "but functions capture whatever I echo"
For example Code:
#!/bin/bash function foo { echo "Hi, I'm function foo" } foo ./foo.sh Hi, I'm function foo Can you post the script that didn't output stuff like it should have? Or make a test case? Alex |
![]() |
![]() |
![]() |
#6 |
Nov 2003
2·1,811 Posts |
![]()
If you want to detect the format of a file use the "file" command. It can detect many image formats, document types, etc.
If you want to learn bash, find a good tutorial and learn from examples. I can recommend you this one (in 3 parts). Regarding escape sequences, avoid them, by converting binary data to strings (e.g., "od" command), and perform comparisons on them. Last fiddled with by Kosmaj on 2009-03-24 at 15:24 |
![]() |
![]() |
![]() |
#7 | |
Aug 2006
5,987 Posts |
![]() Quote:
Code:
foo () { echo -n 'aa' } if [ `foo` = 'aa' ]; then echo Equal else echo Unequal fi exit 0 |
|
![]() |
![]() |
![]() |
#8 | |||
Aug 2006
5,987 Posts |
![]() Quote:
Quote:
![]() Quote:
Code:
match () { # Does the $header start with the argument $1? Useful when characters would # interfere with regexes. len=$((`echo "$1" | wc -c`-1)) left=`echo "$header" | head -c"$len"` echo "\"$left\" = \"$1\"" } . . . header="`head -c999 "$file"`" png=`echo -e '\0211PNG'` MSOffice=`echo -e '\0320\0317\0021\0340\0241\0261\0032\0341'` OOo=`echo -e '\0120\0113\0003\0004\0024\0000'` if [ `match "$png"` ]; then exec_picture elif [ `match "$OOo"` ]; then exec_OOo Last fiddled with by CRGreathouse on 2009-03-24 at 19:50 |
|||
![]() |
![]() |
![]() |
#9 |
"Nancy"
Aug 2002
Alexandria
2,467 Posts |
![]()
The backticks `` make the output to stdout from the command between the backticks appear on the command line, so
foo=`echo -n Hi` is equivalent to foo="Hi" In your case, the output "aa" from the foo function got substituted for `foo`, so the if-line reads if [ "aa" = 'aa' ]; then Normally a single "=" is assignment (without spaces around the "="!), you need a double "==" for comparison. Oddly, in your case the single = seems to do a comparison anyway... The bash syntax can be pretty tricky, especially when variable expansion, quoting, globbing and whatnot are involved... an introductory text like Kosmaj posted would probably be worthwhile. Alex |
![]() |
![]() |
![]() |
#10 | |
Aug 2006
5,987 Posts |
![]() Quote:
Code:
if [ blah ]; then return 0 else return '' fi Code:
if [ blah ]; then return 0 else return 9 fi |
|
![]() |
![]() |
![]() |
#11 |
Nov 2003
2·1,811 Posts |
![]()
A few things;
1) To catch the return value from a function, refer to $? immidiately after calling it. Let's put your blah testing snippet in a funcion called "blahtest", then after blahtest ret=$? you will have 0 or 9 in $ret. It's tricky, that's why it's better to use global varialbes to handle return values. 2) In bash you use single "=" to compare strings. That's why your code works. You can also use "==" but there is something tricky about it, it has different meaning in single brackets and in double brackets (will find you a reference later). It's the best to avoid it for the time being. 3) Binary valules in echo can confuse bash, that's why it's better to use "od" and do string comparisons. Even better, execute "file" and perform string comparisons on its output. |
![]() |
![]() |
![]() |
Thread Tools | |
![]() |
||||
Thread | Thread Starter | Forum | Replies | Last Post |
Scripts thread | bsquared | YAFU | 4 | 2012-10-21 19:45 |
the scripts thread | TimSorbet | Conjectures 'R Us | 52 | 2012-05-29 21:43 |
aliquot escape | firejuggler | Aliquot Sequences | 26 | 2012-01-19 08:15 |
High-altitude driver escape | fivemack | Aliquot Sequences | 1 | 2011-04-24 09:34 |
DPGraph 2D/3D scripts | nibble4bits | Lounge | 0 | 2008-01-16 17:05 |