LinuxQuestions.org
Help answer threads with 0 replies.
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, 06: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, 12:16 PM   #2
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Arch
Posts: 3,013

Rep: Reputation: 1225Reputation: 1225Reputation: 1225Reputation: 1225Reputation: 1225Reputation: 1225Reputation: 1225Reputation: 1225Reputation: 1225
^@ 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, 02:09 PM   #3
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,604

Rep: Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241
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, 02:20 PM   #4
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,258

Rep: Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947
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, 02:32 PM   #5
Batistuta_g_2000
Member
 
Registered: Oct 2011
Posts: 85
Blog Entries: 1

Original Poster
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, 08:14 PM   #6
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,396
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, 01:36 AM   #7
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Arch
Posts: 3,013

Rep: Reputation: 1225Reputation: 1225Reputation: 1225Reputation: 1225Reputation: 1225Reputation: 1225Reputation: 1225Reputation: 1225Reputation: 1225
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, 01:45 AM   #8
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 670Reputation: 670Reputation: 670Reputation: 670Reputation: 670Reputation: 670
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, 06:49 AM   #9
Batistuta_g_2000
Member
 
Registered: Oct 2011
Posts: 85
Blog Entries: 1

Original Poster
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


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 09:11 PM
Error in replacing special chars using awk linux_vidhyarthi Linux - General 2 04-09-2008 08:39 PM
bash, filenames, special chars zomane Programming 3 06-15-2007 05:55 AM
Problem with special chars in general smokylux Linux - General 6 05-26-2004 05:46 AM
telnet and special chars csDraco_ Slackware 7 05-21-2003 10:57 AM


All times are GMT -5. The time now is 06:33 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration