[SOLVED] Do special vi chars lose meaning if copied and pasted in script?
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Do special vi chars lose meaning if copied and pasted in script?
So I have my mutt installer script, looks ok, could be way more intuitive but anyhow, I copied code from VMplayer ubuntu terminal onto windows box notepad and back across and the ^@ (Enter) value was lost when copied back over.
This is an assignment which (because the lecturer is a bit of a lazy-s.o.b) will only be marked if the script works - concerned that the special chars will disappear...any advice appreciated.
Code:
#/!/bin/bash
mkdir -p ~/assignment1/errors/
dpkg -s mutt 2>~/assignment1/errors/error
# checks is mutt is there already
if [[ $? -eq 1 ]]
then
echo "To install mutt email application now press enter"
echo "Or press any key followed by enter to exit to main menu"
read entry1
if [[ "$entry1" == "^@" ]]
then
echo "INSTALLING..........INSTALLING"
echo "______________________________"
sudo apt-get install mutt
else
echo "You need to install Mutt for program to work."
echo "Exiting to main menu"
sleep 3s
fi
else
echo "installed already exiting in"
fi
exit 0
^@ isn't an enter value, it's actually a nul (or 0) byte. Internally, bash uses nul bytes to represent end-of-string, so you can't have it as the value of a string. Therefore, "^@" is the same as "".
Also, your indentation is inconsistent, and you have an extra "/" in your "#!".
Copy/paste is an operation done using the display buffer - therefore it will copy what is displayed, which is not necessarily the same thing as what was typed.
Why are you copy-pasting it into Notepad? Just scp/ftp/email it or stick it on a USB drive. Copy/paste can lose a lot of info like jpollard said. Tabs turn into spaces, special characters turn into their displayed equivalents, etc.
^@ isn't an enter value, it's actually a nul (or 0) byte. Internally, bash uses nul bytes to represent end-of-string, so you can't have it as the value of a string. Therefore, "^@" is the same as "".
Also, your indentation is inconsistent, and you have an extra "/" in your "#!".
Thanks, not too worried about the indententation in script (but should have put in properly to forum), thanks for the shebang error spotted. I have my keyboard config set to UK and when I control-v and hit Enter is comes out ^@ but if I do control-v and leave a second and press enter I get ^M. I tested the ENTER with the following script and result below, does it look ok?
Thanks
Code:
#!/bin/bash
echo "enter command enter"
read entry
if [ $entry == ^@ ] ; then
echo "ENTER"
else
echo "Not ENTER_______"
fi
Any time you start thinking 'special characters' in a shell script, you should be concerned. There aren't really any special characters required in a shell script, and anything that looks like one is more likely to give you grief than joy. When editing a script to run on a Linux host, edit the script on the Linux host. There are special characters in DOS/Windows editors called Carriage Returns which will give you grief.
And, since you're learning about Linux, you should embrace it in its full glory; use it, learn it, love it.
I have my keyboard config set to UK and when I control-v and hit Enter is comes out ^@ but if I do control-v and leave a second and press enter I get ^M. I tested the ENTER with the following script and result below, does it look ok?
It really doesn't matter what happens with control-v Enter (although that behaviour sounds a bit strange), the read command reads a line of input, ie it gets the text up until the a newline is input. If the user presses Enter straight away, then there is no text, so just use
Code:
if [ "$entry" == "" ] ; then
echo "ENTER"
fi
## or
if [ -z "$entry" ] ; then
echo "ENTER"
fi
To reiterate, bash can't see the ^@ character in strings:
There is a utility called dos2unix that will convert DOS nl/return characters to the unix NL character. Scripts entered in notepad won't run until you fix the line endings. You will have the same problems with make files and name=value entries in config files.
You can also use the "od" command to examine control characters in text files. You can also use sed or vim to replace the end characters as well.
Any time you start thinking 'special characters' in a shell script, you should be concerned. There aren't really any special characters required in a shell script, and anything that looks like one is more likely to give you grief than joy. When editing a script to run on a Linux host, edit the script on the Linux host. There are special characters in DOS/Windows editors called Carriage Returns which will give you grief.
And, since you're learning about Linux, you should embrace it in its full glory; use it, learn it, love it.
Good advice, thanks.
---------- Post added 02-20-13 at 11:49 AM ----------
Quote:
Originally Posted by ntubski
It really doesn't matter what happens with control-v Enter (although that behaviour sounds a bit strange), the read command reads a line of input, ie it gets the text up until the a newline is input. If the user presses Enter straight away, then there is no text, so just use
Code:
if [ "$entry" == "" ] ; then
echo "ENTER"
fi
## or
if [ -z "$entry" ] ; then
echo "ENTER"
fi
To reiterate, bash can't see the ^@ character in strings:
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.