LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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


Reply
  Search this Thread
Old 02-19-2013, 05:02 AM   #1
Batistuta_g_2000
Member
 
Registered: Oct 2011
Posts: 85
Blog Entries: 1

Rep: Reputation: Disabled
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
 
Old 02-19-2013, 11:16 AM   #2
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,784

Rep: Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083
^@ 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 "#!".
 
Old 02-19-2013, 01:09 PM   #3
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,912

Rep: Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513
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.
 
Old 02-19-2013, 01:20 PM   #4
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
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.
 
Old 02-19-2013, 01:32 PM   #5
Batistuta_g_2000
Member
 
Registered: Oct 2011
Posts: 85

Original Poster
Blog Entries: 1

Rep: Reputation: Disabled
Quote:
Originally Posted by ntubski View Post
^@ 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
Output

UBUNTU::./tester
enter command enter

ENTER


Could it be wrong?
 
Old 02-19-2013, 07:14 PM   #6
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
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.
 
Old 02-20-2013, 12:36 AM   #7
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,784

Rep: Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083
Quote:
Originally Posted by Batistuta_g_2000 View Post
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:

Code:
~/tmp$ x="foo"
~/tmp$ echo ${#x}
3
~/tmp$ x=""
~/tmp$ echo ${#x}
0
~/tmp$ x="^@"
~/tmp$ echo ${#x}
0
~/tmp$ x="^@^@^@"
~/tmp$ echo ${#x}
0
 
Old 02-20-2013, 12:45 AM   #8
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
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.
 
Old 02-20-2013, 05:49 AM   #9
Batistuta_g_2000
Member
 
Registered: Oct 2011
Posts: 85

Original Poster
Blog Entries: 1

Rep: Reputation: Disabled
Quote:
Originally Posted by theNbomr View Post
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 View Post
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:

Code:
~/tmp$ x="foo"
~/tmp$ echo ${#x}
3
~/tmp$ x=""
~/tmp$ echo ${#x}
0
~/tmp$ x="^@"
~/tmp$ echo ${#x}
0
~/tmp$ x="^@^@^@"
~/tmp$ echo ${#x}
0
Got it, thanks a lot for the help.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: Without Free Software, Open Source Would Lose its Meaning LXer Syndicated Linux News 0 09-28-2009 08:11 PM
Error in replacing special chars using awk linux_vidhyarthi Linux - General 2 04-09-2008 07:39 PM
bash, filenames, special chars zomane Programming 3 06-15-2007 04:55 AM
Problem with special chars in general smokylux Linux - General 6 05-26-2004 04:46 AM
telnet and special chars csDraco_ Slackware 7 05-21-2003 09:57 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 06:04 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration