Solved Set the env for nano

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 :)
 
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".
 
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?
 
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).
 
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"
 
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
 
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
 
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.
 
Back
Top