beginner scripting questions User variables


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting beginner scripting questions User variables
# 1  
Old 12-15-2011
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
Code:
#!/bin/tcsh

or can i leave it as
Code:
#!/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!

Last edited by austing5; 12-15-2011 at 02:15 PM..
# 2  
Old 12-15-2011
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:

Code:
 
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
This User Gave Thanks to BeefStu For This Post:
# 3  
Old 12-15-2011
Quote:
Originally Posted by austing5
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?
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.

Quote:
2)when using "set" command does my top line file have to
Code:
#!/bin/tcsh

or can i leave it as
Code:
#!/bin/sh

?
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!

Quote:
3) How do you assign a user variable in sh or bash?
Easy:
Code:
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:

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

which would export http_proxy only for wget. csh doesn't have that feature.
Quote:
I really need an explanation of do's and dont's
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.

Last edited by Corona688; 12-15-2011 at 03:02 PM..
This User Gave Thanks to Corona688 For This Post:
# 4  
Old 12-15-2011
Quote:
Originally Posted by BeefStu
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:

Code:
 
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
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 ----------

Quote:
Originally Posted by Corona688
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:
Code:
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:
thats why it wasen't working ! "spaces" awesome!!!! thank you!
# 5  
Old 12-15-2011
BASH -- and ksh, and sh -- care about spaces in a few other places as well.

This is wrong:
Code:
if["this"="this"]
then
        echo "strings are identical"
fi

This is okay:
Code:
if [ "this" = "this" ]
then
        echo "strings are identical"
fi

This is also okay:

Code:
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:

Code:
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

Code:
if programname
then
        echo "programname succeeded"
fi

! also works where you might expect it:

Code:
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:

Code:
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Beginner - scripting help!

hi all, i am very new to linux and am trying to create a basic script. I would like the script to copy files from one directory into another, (e.g Script ~/my-documents/fileone ~/my-documents/filetwo) Once all files have been copied, i'd like another script to run automatically and rename... (12 Replies)
Discussion started by: highland
12 Replies

2. Shell Programming and Scripting

[Beginner's questions] Filename Validation & Parsing

Hi !! I'm rather new both to the UNIX and scripting worlds, and I'm learning the ropes of scripting. Having said this, please excuse me if you notice certain basic errors. I'm working on a script that implements .jar and .war files for a WAS environment and I need to perform certain... (4 Replies)
Discussion started by: levaldez
4 Replies

3. AIX

Beginner's questions about AIX (6.1)

Hello, For some time I have intellistation 9111-285 and I installed AIX 6.1 on it. As a complete beginner I have 2 questions in general about AIX and two specific: 1. is the SMS (system management services) part of AIX? As I noticed when I had Yellowdog Linux installed they weren't available?... (2 Replies)
Discussion started by: kenashkov
2 Replies

4. Shell Programming and Scripting

Need help for scripting beginner

hy guys, I have perl script provided to me but i need to convert it into shell .Can you help me in this using sed shell command. cat /etc/passwd |perl -ne '/^(\w+):\w+: (\w+)/ and print "$1, $2\n";' (1 Reply)
Discussion started by: singh_king
1 Replies

5. Homework & Coursework Questions

Beginner Questions.

This is the Test_Data.snp file: MEGAUPLOAD - The leading online storage and file delivery service 1. The problem statement, all variables and given/known data: Problem Set: Before you get started working with these challenges, be aware that the first challenge is reformatting the test data... (7 Replies)
Discussion started by: vlay2
7 Replies

6. Linux

Beginner questions about versions and installing

1. I have never used or dealt with unix, linux, or any variation thereof, and want to. My biggest problem is that all the versions I've looked at want you to install from a CD or DVD, but I'm wanting to put it onto an Asus eeepc, which has no such drive. How would I go about installing on it? ... (4 Replies)
Discussion started by: lemming
4 Replies

7. UNIX for Dummies Questions & Answers

Absolute Beginner Questions

... before you role your eyes, I picked up my first Unix book 3 days ago! As such, I have a few quick questions that I'm sure are super easy for everyone out there but me! Forgive me if the terminology I use is wrong ... I'm accessing a remote Unix server, I can make my way around directories... (2 Replies)
Discussion started by: joey_tomatoes
2 Replies

8. UNIX for Dummies Questions & Answers

Beginner Questions

Hi everyone. I guess I am the new guy, and also new to Unix. I purchased a box of computer supplies at an auction, and found an unopened box of Compaq Smartstart. So here is what I have... Smartstart 2.5, Netware, Windows NT, OS2 & Lan Server, SCO Open Server release, SCO Unixware 2, Oracle 7 for... (3 Replies)
Discussion started by: Darin
3 Replies
Login or Register to Ask a Question