sed | read var | echo -n | echo $! |


Commands to show hidden characters
sed -n 'l' f  #l - L
-----
sed commands usually include backslashes

sed s/text/replace/g file > newfile
-----------------------------------
There are three primary ways of assigning a value to a variable:
.. Explicit definition: VAR=value
.. Read: read VAR
.. Command substitution: VAR=`date`, VAR=$(date)
cat f
#!/bin/sh
echo -n please enter your name:\
read var
echo your name is $var
-----------------------------------
cat f
#!/bin/sh
echo -n please enter your name:\
read var
echo your name is $var
Please enter your first name and last name: AAA VVV
Hello, AAA. How is the VVV family?
{{PAGE 36}}
-----------------------------------
reading from files
$ while read message
> do
> echo $message
> done < /etc/motd
-----------------------------------
date +"%A"
Tuesday
date +"%k"' '"%A"
11 Tuesday
-----------------------------------
cat par.sh
echo name of command is $0
echo first par $1
echo second par $2
echo how many parameters provided $#
./par.sh 1 2 3
name of command is ./par.sh
first par 1
second par 2
how many parameters provided 3
The variables $0 through $9 are defined, $10 does not exist and (even though this is inconsistent
with other types of variables) is interpreted as $1 followed by a zero.
-----------------------------------
The shift builtin command moves everything along by one, dropping $1, then $2, then
$3, and so on, each time it gets called.
cat par.sh
echo name of command is $0
echo first par $1
echo second par $2
shift
shift
shift
echo how many parameters provided $#
./par.sh 1 2 3
name of command is ./par.sh
first par 1
second par 2
how many parameters provided 0
-----------------------------------
The shell sets the $? variable to be the return code of the last-run command
-----------------------------------
sleep 30 &
[1] 5850
$ echo $!
5850
you can run processes in the background and find their process ID (PID) in the $! variable
-----------------------------------
sleep 50 &
[1] 12295
$ strace -p $!
Process 12295 attached - interrupt to quit
restart_syscall(<... resuming interrupted call ...>) = 0
close(1)                                = 0
exit_group(0)                           = ?
Process 12295 detached
[1]+  Done                    sleep 50
-----------------------------------
BASH_SOURCE, FUNCNAME, LINENO and BASH_LINENO are incredibly useful debugging
variables that tell you just where you are in the script, even when you have multiple files in use
Some variables cannot be unset; these are referred to as the “read-only” variables.
You can’t unset $1, $2, $#, and so on
$ cat lineno.sh
#!/bin/bash
echo “Hello, World”
echo “This is line $LINENO”
$ ./lineno.sh
Hello, World
This is line 4
$
This can be useful for debugging and indeed for getting useful information from end users. Instead
of reporting “Error occurred in the fourth debugging point,” your script can give the exact line
number, giving you pinpoint accuracy in the diagnosis of any problems with the script
-----------------------------------

Comments

Popular posts from this blog

HAproxy logging

teamcity Automatic Agent Start under Linux

NFS mount add in fstab _netdev instead of default | firewall-cmd --list-all