LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 01-23-2010, 03:01 PM   #1
shanie
LQ Newbie
 
Registered: Apr 2009
Posts: 8

Rep: Reputation: 0
Question Shell scripting help


Hi i am trying to learn shell scripting


what does the dollar sign mean, and these signs {}


DEST="${DST}`date +%F`/"


Thanks
 
Old 01-23-2010, 03:58 PM   #2
onebuck
Moderator
 
Registered: Jan 2005
Location: Midwest USA, Central Illinois
Distribution: Slackware®
Posts: 12,716
Blog Entries: 27

Rep: Reputation: 2058Reputation: 2058Reputation: 2058Reputation: 2058Reputation: 2058Reputation: 2058Reputation: 2058Reputation: 2058Reputation: 2058Reputation: 2058Reputation: 2058
Hi,

I can't explain it any better than;

Quote:
excerpt from 'Bash Reference Manual';

3.4.1 Positional Parameters

A positional parameter is a parameter denoted by one or more digits, other than the single digit 0. Positional parameters are assigned from the shell's arguments when it is invoked, and may be reassigned using the set builtin command. Positional parameter N may be referenced as ${N}, or as $N when N consists of a single digit. Positional parameters may not be assigned to with assignment statements. The set and shift builtins are used to set and unset them (see Shell Builtin Commands). The positional parameters are temporarily replaced when a shell function is executed (see Shell Functions).

When a positional parameter consisting of more than a single digit is expanded, it must be enclosed in braces.

3.4.2 Special Parameters

The shell treats several parameters specially. These parameters may only be referenced; assignment to them is not allowed.

*
Expands to the positional parameters, starting from one. When the expansion occurs within double quotes, it expands to a single word with the value of each parameter separated by the first character of the IFS special variable. That is, "$*" is equivalent to "$1c$2c...", where c is the first character of the value of the IFS variable. If IFS is unset, the parameters are separated by spaces. If IFS is null, the parameters are joined without intervening separators.
@
Expands to the positional parameters, starting from one. When the expansion occurs within double quotes, each parameter expands to a separate word. That is, "$@" is equivalent to "$1" "$2" .... If the double-quoted expansion occurs within a word, the expansion of the first parameter is joined with the beginning part of the original word, and the expansion of the last parameter is joined with the last part of the original word. When there are no positional parameters, "$@" and $@ expand to nothing (i.e., they are removed).
#
Expands to the number of positional parameters in decimal.
?
Expands to the exit status of the most recently executed foreground pipeline.
-
(A hyphen.) Expands to the current option flags as specified upon invocation, by the set builtin command, or those set by the shell itself (such as the -i option).
$
Expands to the process id of the shell. In a () subshell, it expands to the process id of the invoking shell, not the subshell.
!
Expands to the process id of the most recently executed background (asynchronous) command.
0
Expands to the name of the shell or shell script. This is set at shell initialization. If Bash is invoked with a file of commands (see Shell Scripts), $0 is set to the name of that file. If Bash is started with the -c option (see Invoking Bash), then $0 is set to the first argument after the string to be executed, if one is present. Otherwise, it is set to the filename used to invoke Bash, as given by argument zero.
_
(An underscore.) At shell startup, set to the absolute pathname used to invoke the shell or shell script being executed as passed in the environment or argument list. Subsequently, expands to the last argument to the previous command, after expansion. Also set to the full pathname used to invoke each command executed and placed in the environment exported to that command. When checking mail, this parameter holds the name of the mail file.

...

3.5.3 Shell Parameter Expansion


The ‘$’ character introduces parameter expansion, command substitution, or arithmetic expansion. The parameter name or symbol to be expanded may be enclosed in braces, which are optional but serve to protect the variable to be expanded from characters immediately following it which could be interpreted as part of the name.

When braces are used, the matching ending brace is the first ‘}’ not escaped by a backslash or within a quoted string, and not within an embedded arithmetic expansion, command substitution, or parameter expansion.

The basic form of parameter expansion is ${parameter}. The value of parameter is substituted. The braces are required when parameter is a positional parameter with more than one digit, or when parameter is followed by a character that is not to be interpreted as part of its name.

If the first character of parameter is an exclamation point, a level of variable indirection is introduced. Bash uses the value of the variable formed from the rest of parameter as the name of the variable; this variable is then expanded and that value is used in the rest of the substitution, rather than the value of parameter itself. This is known as indirect expansion. The exceptions to this are the expansions of ${!prefix*} and ${!name[@]} described below. The exclamation point must immediately follow the left brace in order to introduce indirection.

In each of the cases below, word is subject to tilde expansion, parameter expansion, command substitution, and arithmetic expansion.

When not performing substring expansion, using the form described below, Bash tests for a parameter that is unset or null. Omitting the colon results in a test only for a parameter that is unset. Put another way, if the colon is included, the operator tests for both parameter's existence and that its value is not null; if the colon is omitted, the operator tests only for existence.

....
You should look at the 'Bash Reference Manual' for detailed explaination(s) that are complete.



Just a few links to aid you;

Linux Documentation Project
Rute Tutorial & Exposition
Linux Command Guide
Utimate Linux Newbie Guide
LinuxSelfHelp
Getting Started with Linux
Bash Reference Manual
Advanced Bash-Scripting Guide
Linux Home Networking


The above links and others can be found at 'Slackware-Links'. More than just Slackware® links!
 
Old 01-23-2010, 03:59 PM   #3
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
To learn shell scripting, first you have to read a lot. The $ sign and the brackets {} are the basics to manage variables and their values in shell programming.

From the Advanced Bash Scripting Guide, chapter 4.1 (variable substitution)
Code:
The name of a variable is a placeholder for its value, the data it holds.
Referencing (retrieving) its value is called variable substitution.

$

    Let us carefully distinguish between the name of a variable and its value.
If variable1 is the name of a variable, then $variable1 is a reference to its
value, the data item it contains.
And from chapter 9.3 (parameter substitution)
Code:
${parameter}

    Same as $parameter, i.e., value of the variable parameter. In certain contexts, only
    the less ambiguous ${parameter} form works.

    May be used for concatenating variables with strings.
Indeed, the "advanced bash" is a bit... advanced, but you can take it as a good reference manual. Other texts are available on-line for beginners (look for Rute and for the Bash Guide for Beginners, just to cite two of them).
 
Old 01-23-2010, 04:01 PM   #4
tronayne
Senior Member
 
Registered: Oct 2003
Location: Northeastern Michigan, where Carhartt is a Designer Label
Distribution: Slackware 32- & 64-bit Stable
Posts: 3,541

Rep: Reputation: 1060Reputation: 1060Reputation: 1060Reputation: 1060Reputation: 1060Reputation: 1060Reputation: 1060Reputation: 1060
The $ evokes parameter expansion; read $ as "value of." In your example, the parameter is DST and ${DST} is the value of DST.

{ } braces are optional, except for a:
  • parameter followed by a letter, digit or underscore that is not to be interpreted as part of its name.
  • Variable that is subscripted.
  • Positional parameter of more than one digit.
(You can use arrays, thus the subscript -- leave that to you to read about in, say, the manual page for BASH or Korn Shell).

Hope this helps some.
 
Old 01-23-2010, 04:12 PM   #5
bmxcess
Member
 
Registered: Jan 2009
Location: Brisbane, Australia
Distribution: @work:RHEL 5.4/Fedora 13, @home:slack64-current,ubuntu lynx studio
Posts: 65

Rep: Reputation: 19
The ${} allows you to reference the value of the variable $DST within the literal string. This is only required if there is an alpha numeric character immediately after your variable in the literal string. e.g

brendan@ubuntu:~$ DST="Some text"
brendan@ubuntu:~$ echo "$DST followed by some more text"
Some text followed by some more text
brendan@ubuntu:~$ echo "$DSTimmediately followed by some more text"
followed by some more text
brendan@ubuntu:~$ echo "${DST}immediately followed by some more text"
Some textimmediately followed by some more text

The second echo command is trying to reference the value of variable $DSTimmediately rather than $DST.
 
Old 01-24-2010, 01:02 AM   #6
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
Quote:
Originally Posted by tronayne View Post
{ } braces are optional, except for a:
  • [snip]
  • Positional parameter of more than one digit.
That was surprising but testing showed it not to be true for bash 3.1:
Code:
c:/tmp$ cat essay.sh
#!/bin/bash
echo $10
c:/tmp$ ./essay.sh 1 2 3 4 5 6 7 8 9 10
10
c:/tmp$ bash --version
GNU bash, version 3.1.17(2)-release (i486-slackware-linux-gnu)
Copyright (C) 2005 Free Software Foundation, Inc.
Old bourne-based shells could not access positional parmaters beyond $9; accessing them required shifting them to lower numbers; perhaps the requirement to use {} with positional parameters of more than one digit was an interim solution for that.
 
Old 01-24-2010, 02:43 AM   #7
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
Quote:
Originally Posted by catkin View Post
That was surprising but testing showed it not to be true for bash 3.1
Hmmm... I think you're misleaded by the choice of the arguments...
Code:
$ ./essay.sh u 2 3 4 5 6 7 8 9 10
u0
Cheers!
Alex
 
Old 01-24-2010, 02:51 AM   #8
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
Quote:
Originally Posted by colucix View Post
Hmmm... I think you're misleaded by the choice of the arguments...
Code:
$ ./essay.sh u 2 3 4 5 6 7 8 9 10
u0
Cheers!
Alex
Thanks Alex
 
Old 01-24-2010, 04:17 PM   #9
shanie
LQ Newbie
 
Registered: Apr 2009
Posts: 8

Original Poster
Rep: Reputation: 0
Thanks

Thanks alot now i kind of understand, i think
 
Old 01-26-2010, 06:14 PM   #10
shanie
LQ Newbie
 
Registered: Apr 2009
Posts: 8

Original Poster
Rep: Reputation: 0
I am working on my first shell script and i need som help please
I have a folder that i am sharing between my linux server and my windows computer at home with samba.

I want that folder not to take up more than 120 gb of my disk space.

Can anyone help me on the way how do i accomplish this by a shell script.

This is my script so far

#!/bin/bash

echo "The disk space of the directory blabla"
du -h /home/shane/blabla*

What i realise when i excecutes the script is, that it shows me the file size name of every files in that directory.
Can someone tell me how do i just get the total disk space that is used, i don't want to see whats in the directory only
the disc space that is used so far..?

Thanks
 
Old 01-26-2010, 06:52 PM   #11
onebuck
Moderator
 
Registered: Jan 2005
Location: Midwest USA, Central Illinois
Distribution: Slackware®
Posts: 12,716
Blog Entries: 27

Rep: Reputation: 2058Reputation: 2058Reputation: 2058Reputation: 2058Reputation: 2058Reputation: 2058Reputation: 2058Reputation: 2058Reputation: 2058Reputation: 2058Reputation: 2058
Hi,

Look at some of the links that I provided. You need to familiarize yourself with the basic commands.

If you want to know the size available for the filesystems then do a;
Code:
~#df -h
'man df' to understand the options;

Quote:
excerpt 'man df';

-h, --human-readable
print sizes in human readable format (e.g., 1K 234M 2G)
You can always do a 'man command' from the cli/console.

 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
LXer: Terminal functions for shell scripting with Shell Curses LXer Syndicated Linux News 0 03-26-2008 11:50 PM
SHELL scripting/ shell functions mayaabboud Linux - Newbie 6 12-26-2007 08:18 AM
Shell Scripting: Getting a pid and killing it via a shell script topcat Programming 15 10-28-2007 02:14 AM
teaching shell scripting: cool scripting examples? fax8 Linux - General 1 04-20-2006 04:29 AM
shell interface vs shell scripting? I'm confused jcchenz Linux - Software 1 10-26-2005 03:32 PM


All times are GMT -5. The time now is 03:50 AM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration