Solved Set the env for nano

Sessa

Member

Reaction score: 1
Messages: 44

Hi Guys,

I installed the editor nano on my FreeBSD. Now I want to use this in a script. For this I install nano and would like to check if it can be found under /usr/locale/bin/nano. If this can be found there, I would like to define the Tabspace. Have to date the following script:

Bash:
#!/bin/bash
if [ -f "/usr/locale/bin/nano" ]; then
    setenv EDITOR /usr/locale/bin/nano
    echo "set tabsize 4
    set tabstospaces" >> ~/.nanorc
else
    echo "Nope"
    exit 1
fi
However, he always jumps directly into the else branch. I suspect that here the "-f" query does not work, because it is not directly a file, right?

Thank you for help :)
 

roccobaroccoSC

Well-Known Member

Reaction score: 96
Messages: 426

One remark: in the first line use /usr/local/bin/bash. This is the bash location on FreeBSD.

And to get back to your question. You have a typo in the condition. You spelled "local" incorrectly as "locale".
 
OP
OP
S

Sessa

Member

Reaction score: 1
Messages: 44

Oh ... Thank you!
Now the script runs into the if. But, the output is init.sh: setenv: not found
But when I enter the single command in the terminal, I get no error message?
 

SirDice

Administrator
Staff member
Administrator
Moderator

Reaction score: 7,681
Messages: 30,610

This is such a simple script, just use /bin/sh instead. There absolutely no need for bash.

Three notes. First, check for executable, not the existence of the file
Code:
if [ -x /usr/local/bin/nano ]; then
If the binary isn't executable anything that uses EDITOR would fail.

Second:
Code:
    echo "set tabsize 4
    set tabstospaces" >> ~/.nanorc
This adds the lines each and every time the script is run. After a while you could end up with hundreds of the same lines added. Just create the ~/.nanorc file and leave it at that. Your echo is also spread over two lines, that's not going to work. Use a heredoc:
Code:
echo << EOF >> ~/.nanorc
set tabsize 4
set tabstospaces
EOF
Thirdly,
Code:
setenv EDITOR /usr/locale/bin/nano
This is csh(1) syntax, not sh(1).
 
OP
OP
S

Sessa

Member

Reaction score: 1
Messages: 44

When i try the script as follows

Bash:
#!/bin/sh
if [ -x /usr/local/bin/nano ]; then
    echo << EOF >> ~/.nanorc
    set tabsize 4
    set tabstospaces
    EOF
else
    echo "Nope"
    exit 1
fi
I received the error: init.sh: 11: Syntax error: end of file unexpected (expecting "fi")

I don't understand this message because of i already set the "fi"
 

tommiie

Active Member

Reaction score: 65
Messages: 220

EDIT: My bad. Python (and possibly other programming languages) use the character three times, /bin/sh uses it only twice.

I believe you need three times the character '<' instead of two:

Bash:
echo <<< EOF >> ~/.nanorc
    set tabsize 4
    set tabstospaces
    EOF
 

roccobaroccoSC

Well-Known Member

Reaction score: 96
Messages: 426

Do not indent the last EOF. It must be at the beginning of the line like so:
Bash:
#!/bin/sh
if [ -x /usr/local/bin/nano ]; then
    echo << EOF >> ~/.nanorc
set tabsize 4
set tabstospaces
EOF
else
    echo "Nope"
    exit 1
fi
 

roccobaroccoSC

Well-Known Member

Reaction score: 96
Messages: 426

I believe you need three times the character '<' instead of two:
No, two is the right number of '<'. The error is that EOF is not in the beginning of the line, so cat cannot recognize it as end of input.
 

roccobaroccoSC

Well-Known Member

Reaction score: 96
Messages: 426

You know that your script will just append the two lines to ~/.nanorc every time you start it, right?
If you execute the script each time, you should probably either overwrite the whole file or add the lines just when they are not in it.
 
Top