LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Shell scripting help (https://www.linuxquestions.org/questions/linux-newbie-8/shell-scripting-help-784249/)

shanie 01-23-2010 03:01 PM

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

onebuck 01-23-2010 03:58 PM

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.

:hattip:

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

:hattip:
The above links and others can be found at 'Slackware-Links'. More than just Slackware® links!

colucix 01-23-2010 03:59 PM

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).

tronayne 01-23-2010 04:01 PM

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.

bmxcess 01-23-2010 04:12 PM

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.

catkin 01-24-2010 01:02 AM

Quote:

Originally Posted by tronayne (Post 3837985)
{ } 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.

colucix 01-24-2010 02:43 AM

Quote:

Originally Posted by catkin (Post 3838325)
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

catkin 01-24-2010 02:51 AM

Quote:

Originally Posted by colucix (Post 3838383)
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

:doh: Thanks Alex :)

shanie 01-24-2010 04:17 PM

Thanks
 
Thanks alot :) now i kind of understand, i think:hattip:

shanie 01-26-2010 06:14 PM

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

onebuck 01-26-2010 06:52 PM

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.

:hattip:


All times are GMT -5. The time now is 11:19 PM.