LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 09-04-2008, 12:53 PM   #1
budhax
LQ Newbie
 
Registered: Aug 2008
Location: Switzerland - GVA
Distribution: BT3
Posts: 8

Rep: Reputation: 0
Question Basic menu.sh fails: strange behavior on echo command.


Hello,
Here is my basic bash script "menu.sh" launching some commands, under KDE 3.5.7, Linux BT 2.6.21.5, Bash 3.1.17.
http://www.airfirst.ch/b2/menu.sh
And the output below.

STEP_A
First run: it fails on the 1st line.

STEP_B : I remove the first line: #!/bin/bash
Then it fails printing commands 03,05,06,08,11.

STEP_C : I replaced "$CN1" by its value "eth1" for commands 03,05,06,08,11.
Then it still fails printing commands 11.


1. How to fix the 1st line error? -bash: ./menu.sh: /bin/bash^M: bad interpreter: No such file or directory
Is tha because the file was created under MS Windows (then get it from VMware on a NT server)?
In VMware, if I create this file, I don't get the error on #!/bin/bash
Code:
#!/bin/bash
echo blabla

2. How to fix this echo printing bugs? It looks that only strings with something after a variable fail.
com03="dir $CN1 -all" : there is something after variable $CN1
com08="ifconfig $CN1 up hw ether $MAC" : there is something after variable $CN1
com12="echo --test -e $ESSID -a $BSSID $CN1" : OK because (maybe) $ESSID, $BSSID are not yet defined.


3. What's wrong in line 31?



4. What could be the crazy reason of this:
- Test to run menu1.sh, and works fine.
- Using Kate editor: select all content in menu1.sh, and paste it in empty menu.sh.
- Test to run menu.sh, and get error:
Code:
-bash: ./menu.sh: /bin/bash^M: bad interpreter: No such file or directory
Should I say "Damned Linux" or "Damned Bash" or what else ????????

Thanks and regards.



PS:
This menu1 works fine under Bash 3.1.17, Linux debbi 2.6.23 (no error):
http://www.airfirst.ch/b2/menu1.sh
http://www.airfirst.ch/b2/outpu_menu1.txt



----------------------------------------------------------------------------------------------------
STEP_A
Code:
bt Desktop # menu.sh
-bash: ./menu.sh: /bin/bash^M: bad interpreter: No such file or directory

STEP_B: I remove the 1st line: #!/bin/bash

Code:
bt Desktop # menu.sh
: command not found
: command not found
./menu.sh: line 13: menu: command not found

********************* TEST MENU
INIT
 01  ls
 02  dir eth1
 -alldir eth1
 04  iwlist eth1
 scaniwlist eth1
 mode monitor eth1
 07  echo start eth1
 up hw ether 50:41:32:23:14:05
MAIN
 09  Set ESSID: []
 10  Set BSSID: []
 eth1echo --write file
 12  echo --test -e  -a  eth1
Choix? 01
': not a valid identifier `com
'/menu.sh: line 31: syntax error near unexpected token `in
'/menu.sh: line 31: `  case "$com" in
STEP_C: I replaced "$CN1" by its value "eth1" for commands 03,05,06,08,11.


Code:
bt Desktop # menu.sh
: command not found
: command not found
./menu.sh: line 13: menu: command not found

********************* TEST MENU
INIT
 01  ls
 02  dir eth1
 03  dir eth1 -all
 04  iwlist eth1
 05  iwlist eth1 scan
 06  iwconfig eth1 mode monitor
 07  echo start eth1
 08  ifconfig eth1 up hw ether 50:41:32:23:14:05
MAIN
 09  Set ESSID: []
 10  Set BSSID: []
 eth1echo --write file
 12  echo --test -e  -a  eth1
Choix? 01
': not a valid identifier `com
'/menu.sh: line 31: syntax error near unexpected token `in
'/menu.sh: line 31: `  case "$com" in
menu.sh

Code:
#!/bin/bash

#------------------------------------------------------------------------------------ Initialisation
DFN=file
CN1=eth1
CN2=rausb0
RTAP0=rtap0
MESSID=IPW22
FIP_100=192.168.1.100
FIP_101=192.168.1.101
MAC=50:41:32:23:14:05
#----------------------------------------------------------------------------------------- Functions
menu {
  echo 
  echo "********************* TEST MENU"
  echo "INIT"
    com01="ls"                                              ; echo " 01  $com01"
    com02="dir $CN1"                                        ; echo " 02  $com02"
    com03="dir $CN1 -all"                                   ; echo " 03  $com03"
    com04="iwlist $CN1"                                     ; echo " 04  $com04"
    com05="iwlist $CN1 scan"                                ; echo " 05  $com05"
    com06="iwconfig $CN1 mode monitor"                      ; echo " 06  $com06"
    com07="echo start $CN1"                                 ; echo " 07  $com07"
    com08="ifconfig $CN1 up hw ether $MAC"                  ; echo " 08  $com08"
  echo "MAIN"
    com09="Set ESSID: [$ESSID]"                             ; echo " 09  $com09"
    com10="Set BSSID: [$BSSID]"                             ; echo " 10  $com10"
    com11="echo --write $DFN $CN1"                          ; echo " 11  $com11"
    com12="echo --test -e $ESSID -a $BSSID $CN1"            ; echo " 12  $com12"
  read -p "Choix? " com
  case "$com" in
    01) eval "$com01";;
    02) eval "$com02";;
    03) eval "$com03";;
    04) eval "$com04";;
    05) eval "$com05";;
    06) eval "$com06";;
    07) eval "$com07";;
    08) eval "$com08";;
    09) read -p "ESSID ? " ESSID;;
    10) read -p "BSSID ? " BSSID;;
    11) eval "$com11";;
    12) eval "$com12";;
    * ) echo "BYE ...."; exit;;
  esac
}
#---------------------------------------------------------------------------------------------- BODY
while true; do menu
done
exit
#---------------------------------------------------------------------------------------------------

Last edited by budhax; 09-04-2008 at 01:03 PM.
 
Old 09-04-2008, 01:59 PM   #2
rocket357
Member
 
Registered: Mar 2007
Location: 127.0.0.1
Distribution: OpenBSD-CURRENT
Posts: 460
Blog Entries: 67

Rep: Reputation: 70
MS Windows uses an extra character in newlines. You can fix this by doing this:

$ sed 's/.$//' <script_name> > <script_name>.FIXED
Test the script here...
$ mv <script_name>.FIXED <script_name>

Once you do that, test again and see if the other errors go away...if not, post here again.
 
Old 09-04-2008, 08:44 PM   #3
chrism01
Guru
 
Registered: Aug 2004
Location: Brisbane
Distribution: Centos 6.2, Centos 5.8
Posts: 11,696

Rep: Reputation: 900Reputation: 900Reputation: 900Reputation: 900Reputation: 900Reputation: 900Reputation: 900Reputation: 900
or use dos2unix . fix the line endings first definitely.
Better still, edit Linux files on Linux....
 
Old 09-05-2008, 06:04 PM   #4
budhax
LQ Newbie
 
Registered: Aug 2008
Location: Switzerland - GVA
Distribution: BT3
Posts: 8

Original Poster
Rep: Reputation: 0
Coool ,all problems fixed.
Thanks guys, both methods (sed, dos2unix) works fine. My script works as expected.
Thank you very much.
 
  


Reply

Tags
bash, script


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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Glade3 strange behavior, menu items disappear sparker Programming 0 03-01-2008 11:44 PM
Strange Behavior - chown command Ingla Linux - General 8 01-28-2007 04:46 AM
Basic Question about update-menu -9 command Rjones1981 Linux - Newbie 0 03-10-2004 08:20 AM
Strange Behavior andrewb758 Linux - Hardware 5 08-31-2003 02:42 PM
strange behavior abhijit Linux - General 3 07-09-2003 11:25 PM


All times are GMT -5. The time now is 01:15 PM.

Main Menu
 
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
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration