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 |
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. |
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............ |
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 |
All times are GMT -5. The time now is 10:07 AM. |