Bash script: Difference between revisions

From HPCWIKI
Jump to navigation Jump to search
No edit summary
No edit summary
Line 6: Line 6:
|-
|-
|set
|set
|'''set [options] [arguments]'''  
|'''set [options] [arguments]'''<ref>https://linuxhint.com/set-command-bash/</ref>
sign (-) is used with the command’s option to enable that option and the plus sign (+) is used with the command’s option to disable that option
sign (-) is used with the command’s option to enable that option and the plus sign (+) is used with the command’s option to disable that option
{| class="wikitable"
{| class="wikitable"
| -a
| -a
|It defines those variables or functions which are created or modified or exported.
|It defines those variables or functions which are created or modified or exported
Example, These variables v1 and v2 can be accessed after executing the script
<code>#!/bin/bash</code>
 
<code>#Enable -a option to read the values of the variables</code>
 
<code>set -a</code>
 
<code>#Initialize three variables</code>
 
<code>v1=78</code>
 
<code>v2=50</code>
|-
|-
| -b
| -b
Line 19: Line 31:
|-
|-
| -C
| -C
|It disables the overwriting feature of the existing file.
|It disables the overwriting feature of the existing file
Example,
$ cat > testfile.txt
 
$ set -C
 
$ cat > testfile.txt.  # Could not overwrite testfile.txt
|-
|-
| -e
| -e
Line 40: Line 58:
|-
|-
| -u
| -u
|It traces the unset variables.
|It traces the unset variables - error is printed for the uninitialized variable
|-
|-
| -v
| -v
Line 47: Line 65:
| -x
| -x
|It displays the commands and their attributes sequentially. It is mainly used to debug the script.
|It displays the commands and their attributes sequentially. It is mainly used to debug the script.
|-
| -- variable
|The string value is divided into three parts based on the space that is printed
<nowiki>#</nowiki>!/bin/bash
<nowiki>#</nowiki>Define a string variable
myvar="Learn bash programming"
<nowiki>#</nowiki>Set the set command without option and with variable
set -- $myvar
<nowiki>#</nowiki>Print the split value
printf "$1\n$2\n$3\n"
|}
|}
'''Exit Values of Set Command'''
# Zero (0) is returned to complete the task successfully.
# One (1) is returned if a failure occurs for any invalid argument.
# One (1) is returned if a failure occurs for a missing argument.
|}
|}
== Reference ==
<references/>

Revision as of 13:16, 28 March 2023

A summary for some important Bash scripts

Command Description
set set [options] [arguments][1]

sign (-) is used with the command’s option to enable that option and the plus sign (+) is used with the command’s option to disable that option

-a It defines those variables or functions which are created or modified or exported

Example, These variables v1 and v2 can be accessed after executing the script #!/bin/bash

#Enable -a option to read the values of the variables

set -a

#Initialize three variables

v1=78

v2=50

-b It informs the job termination.
-B To do the task of the brace expansion.
-C It disables the overwriting feature of the existing file

Example, $ cat > testfile.txt

$ set -C

$ cat > testfile.txt. # Could not overwrite testfile.txt

-e It exits for non-zero exit status value.
-f It disables the filename generation task.
-h It saves the location of the command where it has been used.
-m It enables job control.
-n It reads the commands.
-t It exits from the command after executing a single command.
-u It traces the unset variables - error is printed for the uninitialized variable
-v It prints the shell input lines.
-x It displays the commands and their attributes sequentially. It is mainly used to debug the script.
-- variable The string value is divided into three parts based on the space that is printed

#!/bin/bash

#Define a string variable

myvar="Learn bash programming"

#Set the set command without option and with variable

set -- $myvar

#Print the split value

printf "$1\n$2\n$3\n"

Exit Values of Set Command

  1. Zero (0) is returned to complete the task successfully.
  2. One (1) is returned if a failure occurs for any invalid argument.
  3. One (1) is returned if a failure occurs for a missing argument.

Reference