LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
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
  Search this Thread
Old 02-15-2012, 07:15 PM   #1
playonmonkey
LQ Newbie
 
Registered: Feb 2012
Posts: 3

Rep: Reputation: Disabled
bash scripting


#!/bin/bash
VAR1=file name
echo "Enter a File name and press Enter:"
read VAR1
echo "file created: $VAR1"
touch $VAR1

root@ubuntu:~# ./lab7parta.sh
./lab7parta.sh: line 2: name: command not found

Enter a File name and press Enter:
joe
You typed: joe
./lab7parta.sh: line 6: var1: command not found

how do i fix this
 
Old 02-15-2012, 07:31 PM   #2
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,649
Blog Entries: 4

Rep: Reputation: 3934Reputation: 3934Reputation: 3934Reputation: 3934Reputation: 3934Reputation: 3934Reputation: 3934Reputation: 3934Reputation: 3934Reputation: 3934Reputation: 3934
If you choose to monkey-around with "good ol' bash" ... then you will for the time being have to content yourself with Bash's somewhat-extremely-limited viewpoint with regard to the entire world.

your choice ...

Bash doesn't understand "VAR1=file name" ...

... because Bash thinks a whole lot more about blank spaces ...

... than it does about newline (\n).

You see, Bash looks at your string as consisting of the following fifteen ASCII characters: VAR1=file(blank_space)name(newline)

"That's it. That's all."

"It's just a computer program, after all, therefore it goes without saying that it is rather incredibly simple."

Quote:
And, to be completely fair, it was written by Brian Fox in 1989 (why, it was only yesterday... seriously!) "as a replacement for the Bourne shell." (I do hereby beg your indulgent indulgence, Brian, and I trust that you know I am not hereby besmirching you, my esteemed colleague ...)
... To fully understand the sometimes bizarre error messages that a computer program now produces, you do need to appreciate its designers' perspective at the time.

(At the time, trust me on this, they made perfect sense. Meanwhile, "deal with it.")

Last edited by sundialsvcs; 02-15-2012 at 07:37 PM.
 
Old 02-15-2012, 07:34 PM   #3
playonmonkey
LQ Newbie
 
Registered: Feb 2012
Posts: 3

Original Poster
Rep: Reputation: Disabled
bash

trust me I dont choose this I have to do it for school and have been messing with it all day and just cant get it if i could what the file should like then i would be able to figure out what i did wrong
 
Old 02-16-2012, 01:10 AM   #4
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
When you want to assign a value including a space to a variable, you need to put it in single quotes: foo='value with spaces'

That's not the whole story but it will do for starters.

EDIT: and when you want to use a variable which contains spaces, and keep those spaces as spaces within the value, you need to put it in double quotes: touch "$foo"

Last edited by catkin; 02-16-2012 at 01:12 AM.
 
Old 02-17-2012, 10:15 AM   #5
uhelp
Member
 
Registered: Nov 2011
Location: Germany, Bavaria, Nueremberg area
Distribution: openSUSE, Debian, LFS
Posts: 205

Rep: Reputation: 43
Code:
#!/bin/bash

# don't do this
#VAR1=file name
#echo "Enter a File name and press Enter:"
#read VAR1
#echo "file created: $VAR1"
#touch $VAR1

read -p 'Enter 3 file names on a line separted by blanks,  please : '  file1 file2 file3

echo $file1
echo $file2
echo $file3
read "help read"
"help internalCommand" gives help on bash internalCommand.
"read" is a builtin.

For sure it will throw errors in the second line, as this line is interpreted as:
assign VAR=file
execute command "name"

if you want to assign a string containing blanks use:
var=' This is a string with some blanks in it assigned to var.'
echo $var

and: DO NOT USE CAPS for vars.
The environment vars are by convention all in caps.
Using var names ThisIsMyVar will prevent from accidentally changing a environment var.
This is much better style and much more safe!

And some remarks to bash scripting in general:
bash is a shell.
It is NOT a fully featured programming language.
It's primary goal is, to give you a reliable interface to the system.
So that you can easily start commands, deal with the ProcessManagment and so on.
And therefore has to follow the general *nix philosophy.

Blaming it therefore is a kind of shortsighted view.
It is a great programm with lots of astounding features,
to the price of strange syntax.
Keep on learning it.
The reward is very high.

Last edited by uhelp; 02-17-2012 at 10:18 AM.
 
  


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



Similar Threads
Thread Thread Starter Forum Replies Last Post
Reading a bash variable in bash scripting problem freeindy Programming 3 11-27-2008 02:29 AM
Need help with bash scripting Kleedrac Linux - Software 6 05-09-2006 02:55 PM
Bash scripting SWAT Linux - Newbie 2 11-11-2003 03:21 AM
bash scripting -=MaGo=- Programming 16 08-30-2003 07:07 PM
Bash scripting kbeaver Programming 5 07-18-2003 08:35 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:54 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