shell-1

http://www.tldp.org/LDP/abs/html/sha-bang.html

http://www.tldp.org/LDP/abs/html/special-chars.html

# all kinds of command and special characters

In computing, a shebang (also called a sha-bang)——-   #!

#!/bin/sh
#!/bin/bash
#!/usr/bin/perl
#!/usr/bin/tcl
#!/bin/sed -f
#!/bin/awk -f

Each of the above script header lines calls a different command interpreter, be it /bin/sh, the default shell (bash in a Linux system) or otherwise.

Invoking the script

Either:
chmod 555 scriptname (gives everyone read/execute permission) [2]

or
chmod +rx scriptname (gives everyone read/execute permission)

chmod u+rx scriptname (gives only the script owner read/execute permission)

Having made the script executable, you may now test it by ./scriptname. [3] If it begins with a “sha-bang” line, invoking the script calls the correct command interpreter to run it.

As a final step, after testing and debugging, you would likely want to move it to /usr/local/bin (as root, of course), to make the script available to yourself and all other users as a systemwide executable. The script could then be invoked by simply typing scriptname [ENTER] from the command-line.

Chapter 3. Special Characters

Comments. Lines beginning with a # (with the exception of #!) are comments and will not be executed.

A command may not follow a comment on the same line. There is no method of terminating the comment, in order for “live code” to begin on the same line. Use a new line for the next command.

Of course, a quoted or an escaped # in an echo statement does not begin a comment.

Command separator [semicolon]. Permits putting two or more commands on the same line.

echo hello; echo there


if [ -x "$filename" ]; then    #  Note the space after the semicolon.
#+                   ^^
  echo "File $filename exists."; cp $filename $filename.bak
else   #                       ^^
  echo "File $filename not found."; touch $filename
fi; echo "File test complete."

Terminator in a case option [double semicolon].

case "$variable" in
  abc)  echo "\$variable = abc" ;;
  xyz)  echo "\$variable = xyz" ;;
esac

;;&, ;&

Terminators in a case option (version 4+ of Bash).

When considering directory names, a single dot represents the current working directory, and two dots denote the parent directory.

bash$ pwd
/home/bozo/projects

bash$ cd .
bash$ pwd
/home/bozo/projects

bash$ cd ..
bash$ pwd
/home/bozo/
	        echo -n

The dot often appears as the destination (directory) of a file movement command, in this context meaning current directory.

bash$ cp /home/bozo/current_work/junk/* .

Copy all the “junk” files to $PWD.

\

escape [backslash]. A quoting mechanism for single characters.

\X escapes the character X. This has the effect of “quoting” X, equivalent to ‘X’. The \ may be used to quote and , so they are expressed literally.

See Chapter 5 for an in-depth explanation of escaped characters.

/

Filename path separator [forward slash]. Separates the components of a filename (as in /home/bozo/projects/Makefile).

This is also the division arithmetic operator.

\

escape [backslash]. A quoting mechanism for single characters.

`

command substitution. The `command` construct makes available the output of command for assignment to a variable. This is also known as backquotes or backticks.

:

null command [colon]. This is the shell equivalent of a “NOP” (no op, a do-nothing operation). It may be considered a synonym for the shell builtin true. The : command is itself a Bashbuiltin, and its exit status is true (0).

ps -aux|grep mouse|awk ‘{print $2}’

ps -aux 列出所有process的信息,grep抓住,awk 只列出第二列的内容

man command

查看命令的使用方法,用q退出

$

Variable substitution (contents of a variable).

var1=5
var2=23skidoo

echo $var1     # 5
echo $var2     # 23skidoo

A $ prefixing a variable name indicates the value the variable holds.

Example 4-3. Variable Assignment, plain and fancy

#!/bin/bash

a=23              # Simple case
echo $a
b=$a
echo $b

# Now, getting a little bit fancier (command substitution).

a=`echo Hello!`   # Assigns result of 'echo' command to 'a' ...
echo $a
#  Note that including an exclamation mark (!) within a
#+ command substitution construct will not work from the command-line,
#+ since this triggers the Bash "history mechanism."
#  Inside a script, however, the history functions are disabled by default.

a=`ls -l`         # Assigns result of 'ls -l' command to 'a'
echo $a           # Unquoted, however, it removes tabs and newlines.
echo
echo "$a"         # The quoted variable preserves whitespace.
                  # (See the chapter on "Quoting.")

exit 0

4.3. Bash Variables Are Untyped

Unlike many other programming languages, Bash does not segregate its variables by “type.” Essentially, Bash variables are character strings, but, depending on context, Bash permits arithmetic operations and comparisons on variables. The determining factor is whether the value of a variable contains only digits.

find the file/number of line containing “matching_string”

sudo grep -rnw ‘path’ -e “matching_string”

no line folding

In less, it’s called line folding rather than line wrapping.  To set it not to fold, use the -S option:

-S, --chop-long-lines

Causes lines longer than the screen width to be chopped rather than folded. That is, the portion of a long line that does not fit in the screen width is not shown. The default is to fold long lines; that is, display the remainder on the next line.

less(1)


Alternatively, as mentioned in the below comment, if you already opened the file, you can toggle the mode by typing -S (and then Enter for some implementations).

Advertisements
shell-1

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s