Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I'd like to run my script fastex with two possible arguments (g or t), like fastex g.

Based on the argument I want to call geany $file or texstudio $file. This is to choose the editor.

I tried

editor=$1
if ["$1" = "g"]; then
geany "$file".tex
exit 1
else
linha=$(wc -l < "$file".tex)
texstudio --line $linha  "$file".tex
exit 1
fi

but the outuput of fastex.sh g is /usr/bin/fastex.sh: 31: [g: not found and it always opens texstudio. The same if I pass t.

share|improve this question
add comment

2 Answers

up vote 3 down vote accepted
  1. You need space after if [ because [ is actually a command (like test) which reads it's arguments (it may be interpreted by the shell but it may be not). Using [[ makes sure shell's internal logic is used (faster, since no new process is launched). Although shells may interpret the [ and test as builtins, this feature can be turned off with enable (and then a new process really is launched).

  2. = is the comparison operator inside of conditional expressions (both external and builtin) however, it is also the assignment operator elsewhere. While test and = conform to POSIX, using == (recognized in [[) seems to be a reasonable choice whenever one doesn't aspire to POSIX conformance, since it brings a bit more sanity to the shell inconsistency of the context dependent meaning of =.

  3. exit 1 - why effectively discard editor's return value? Either leave it out completely or use exec to replace the script with the editor.


#!/bin/bash
editor=$1
if [[ "$1" == "g" ]]; then
    exec geany "$file.tex"
else
    linha=$(wc -l < "$file.tex")
    exec texstudio --line $linha  "$file.tex"
fi
share|improve this answer
    
Great. I don't know why since I am new on this, but I replaced /bin/sh by /bin/bash as you wrote and your code now works. Thanks. –  Sigur Jan 11 '13 at 19:52
2  
Actually. [ does take =; there is no such thing as assignment re [. –  Ignacio Vazquez-Abrams Jan 11 '13 at 19:54
    
@Sigur Why? On Ubuntu, /bin/sh is a symbolic link to /bin/dash. The best practice in writing the file magic line (aka "shebang" or "hashbang") is this: #!/usr/bin/env bash. –  Christopher Jan 11 '13 at 20:31
2  
I downvoted because 1. is false, and == is not even defined by POSIX. –  jordanm Jan 11 '13 at 20:47
1  
@sigur - that code will not work in dash. It uses non-POSIX features ([[), so /bin/sh is not right. –  jordanm Jan 11 '13 at 20:48
show 12 more comments
if ["$1" = "g"]; then

You need spaces inside [ and ], as they are parsed like ordinary commands. Indeed, [ is an ordinary command (which is a builtin in most shells).

exit 1

Why are you exiting with an error status after running the editor? A return status of 0 means success, anything else means failure. You should make your script return the same status as the editor. As written, you can leave off exit altogether: when the shell reaches the end of the script, it terminates the execution of the script and returns the status of the last command that was executed.

As a refinement, you can use the exec builtin so that instead of running the editor as a subprocess, your script will terminate and be replaced by the editor. (The same process keeps running, but its code switches to that of the other program.) But don't do that if you make this a function and not a script!

A case construct would be clearer to express that you're basing your conditional on the value of a particular variable.

#!/bin/sh
editor=$1 file=$2
case "$editor" in
  g) exec geany "$file".tex;;
  *)
    linha=$(wc -l < "$file".tex)
    exec texstudio --line "$linha" "$file.tex";;
esac
share|improve this answer
    
When I call the script it works, that is, the editor opens the file. But the terminal is locked to other commands. Is it possible to continue using the same terminal while the editor is open? –  Sigur Jan 14 '13 at 22:37
1  
@Sigur Either run fastex g somefile &, or add a & (and don't put exec) on the line that launches the editor, e.g. g) geany "$file.tex" & ;; –  Gilles Jan 14 '13 at 22:41
    
Is it necessary the double quotes around the variable $file which was defined by file=fastex-temp.tex? –  Sigur Jan 14 '13 at 22:45
1  
@Sigur If you know that the variable doesn't contain any whitespace character or any of \[?* (and you haven't changed IFS) then the double quotes aren't necessary. But you can hardly know that in advance when writing the script, so why leave them out? –  Gilles Jan 14 '13 at 23:04
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.