LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 08-12-2004, 01:49 PM   #1
hrg
LQ Newbie
 
Registered: Jul 2004
Posts: 12

Rep: Reputation: 0
Newbie : expr scripting error


Hi Peps

I am learining ...or trying to learn the basics of scripting.

Basically when I try to a create script like the following in my home directory I get an error

cat > first
#
# My first shel scirpt
#
clear
echo "knowledge is power"

crtl+D (this saves my script with the file name as first)

then when I set the permissions to make it executable

chmod 777 first

or

chmod +x first

it gives me the following error

cannot execute binary file

Any ideas???????????

I dont know what to do. Alos when running the expr command the following happens

n=20
y=5
expr n + y

expr : non numeric argument

Not having to much luck with this scripting business..... : (

Can anyone help thanks

Last edited by hrg; 08-12-2004 at 01:51 PM.
 
Old 08-12-2004, 02:19 PM   #2
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
First problem:

You need to start each shell script with a "shebang" line. You probably want this:

#!/bin/bash

The #! tells the system this file contains a script that needs an interpreter. Then the /bin/bash tells the system which interpreter you want to use. In a nutshell, you're saying "This file contains a bunch of commands that I want bash to execute for me". This has to be the very first line of the file; no exceptions. And you'll also need to give execute permissions as well (like you did with the chmod +x command). If you don't want to add the "shebang" line, you will have to execute your script like so:

bash ./first



Second problem:

Your expr statement is trying to add two letters. To get what you want, you would need to do:

expr $n + $y

"n" by itself represents either the name of the variable or just plain text (a normal character). Adding the $ in front says to substitute the value of the variable instead of its name. That's how all variables work in bash scripts.
 
Old 08-12-2004, 03:08 PM   #3
thegeekster
Member
 
Registered: Dec 2003
Location: USA (Pacific coast)
Distribution: Vector 5.8-SOHO, FreeBSD 6.2
Posts: 513

Rep: Reputation: 34
If you want to test some expressions for using in a script, but don't want to run a whole script, you can simply put the command(s) to test in a regular text file, save it as "test", or whatever you want, and then 'source' the text file with the command(s).........

To source a file, you run the command:

source ./test

NOTE: You must use a valid path to the file, such as a full path, or use the shortcut path of ./ to represent the current directory..........

There is shortcut command for source when sourcing a file, which is to replace the word 'source' with a single dot, like so:

. ./test

When sourcing a file, you do not need to make it excecutable with the 'chmod' command, nor do you need to add the "shebang" line (#!...) at the top of the file......

This is a great way to test long or complicated commands without having to type it on the command line, or to refine commands by changing certain commands and testing them, to make sure they work.........


PS: This method is also used in scripts to add variables or extra commands/functions to the script, thus making a script modular................You save certain commands (usually in the form of functions) or variables, which may be commonly used among various scripts, into a separate file and then source that file early in the script............

Last edited by thegeekster; 08-12-2004 at 03:21 PM.
 
Old 08-13-2004, 10:24 AM   #4
hrg
LQ Newbie
 
Registered: Jul 2004
Posts: 12

Original Poster
Rep: Reputation: 0
Thanks Guys

Both posts helped me loads and both worked to the dot.......lol

Anyway Thanks once again for the help it was much appreciated.

Anyways times are wasting back to the scripting
 
  


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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Newbie Needs Help On Scripting 104625 Linux - Software 5 11-27-2011 06:28 PM
Shell scripting help -- newbie rnj Linux - Newbie 1 09-12-2005 12:08 AM
expr syntax error baosheng Programming 4 06-09-2005 03:32 PM
Newbie Scripting, please help granny Programming 6 04-24-2003 11:46 PM
A Newbie,s Scripting Question newpenguin Linux - Newbie 7 10-07-2002 12:52 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 11:46 PM.

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