beginner scripting questions User variables

If there's anywhere to look this up, it would be just as helpful. I googled and really couldn't find anything relative to this.

ok... General Variables

1) When creating a script I made a file "prog1.sh" does it matter if the end is .sh or is this what has to be done like prog.bash or prog.ksh?

2)when using "set" command does my top line file have to

#!/bin/tcsh

or can i leave it as

#!/bin/sh

?

3) How do you assign a user variable in sh or bash?

I really need an explanation of do's and dont's

If this can be found somewhere, or if it's easy enough to answer here.

Thanks!

You can call your script name anything you like. Ie bla.bla xx ....

Typically the first line in the scirpt is the shabang command, this dictates
what shell you are using.. For the most flexibility I would suggest using /bin/ksh Can I assume your other example was /bin/csh and not /bin/tcsh

Lastly you can set a variable like this:

 
value="test"
let int=3
 

These are not terribly difficult questions so the best way to find your answers would be to test... Good luck

1 Like

Linux and UNIX don't care about the filename at all. They check the first few bytes in the file to see what it is.

All the top line does is tell the OS what shell to use.

If you want to use tcsh, it should be #!/bin/tcsh

If you want to use sh, it should be #!/bin/sh

They both have 'set', actually. But they're not the same 'set'. So it depends which programming language you have been writing for!

Easy:

VAR="something"

Note that you can't put spaces between VAR, =, and ". like you might in tcsh. It has to be VAR="... with no spaces at all except inside quotes. This is because of the way BASH lets you set variables before running a program:

http_proxy="localhost:3128" wget www.website.com

which would export http_proxy only for wget. csh doesn't have that feature.

Try the advanced bash scripting guide. Quite a lot of it will apply to any bourne shell.

bash, ksh, and sh are all bourne shells. bash and ksh are different extensions on the basic 'sh', and should still be able to run plain 'sh' code fine. But if you use arrays for example, it won't work in a basic 'sh' shell. If it does on your system, that's often because sh is just a link to bash in some linux systems.

csh and tcsh are definitely not bourne shells at all.

1 Like

The way they teach us has always been /bin/sh i was just seeing what was the difference i'm guessing different shells use different command like "set" and "let" . Thanks!

---------- Post updated at 02:03 PM ---------- Previous update was at 01:58 PM ----------

thats why it wasen't working ! "spaces" awesome!!!! thank you!

BASH -- and ksh, and sh -- care about spaces in a few other places as well.

This is wrong:

if["this"="this"]
then
        echo "strings are identical"
fi

This is okay:

if [ "this" = "this" ]
then
        echo "strings are identical"
fi

This is also okay:

if [[ "this" = "this" ]]
then
        echo "strings are identical"
fi

[[ ]] are extended versions of [ ]. They support the same things [ ] do and more besides. Look for test operators in the advanced bash scripting guide to see what flags you can do, like if [ -d directory ] # Is 'directory' a directory? if [ -z "str" ] # Is "str" a blank string? (no, that's "" )

Also, you can do things like this to process files efficiently without backticks:

while read LINE
do
        echo "$LINE"
done < /path/to/file

you can substitute [ ] style tests for the 'read' statement there. In fact you can put [ ] statements anywhere you'd put a command, and vice versa. [ actually used to be a command, as in, there was actually a /bin/[ file which ran when you did if [ -d directory ] Your system probably still has /bin/[ or something like that somewhere for compatibility, even though it's probably not using it anymore.

This means you can use if to detect whether a command succeeded or not without counting lines or checking its backticks output or anything like that. You can just do

if programname
then
        echo "programname succeeded"
fi

! also works where you might expect it:

if ! programname
then
        echo "programname failed"
fi

---------- Post updated at 01:26 PM ---------- Previous update was at 01:17 PM ----------

Another unique feature the bourne shell has and csh simply cannot do is this:

for X in 1 2 3 4 5
do
        echo "$X"
done > outputfile

You can redirect or pipe the output of entire code blocks, not just individual shell statements.