Conditional command execution -it is possible to specify that a command in a script will only run if particular condition Is met - such conditions are always expressed in terms of the exit status of another program, as follows: Command1 && command2 Means that command2 will only run if command1 Completes with an exit status of 0 Command3 || command4 Means that command4 will only run if command3 completes with an exit status that is not 0 For example: ls file1 && cp file1 ./tmp cp abc xyz && echo The file was copied OK diff fileA fileB || echo The fiels are different ls file2 || exit -The only problem with these constructs is that they are very limited: - You can only perform one command if the condition is met (however, it is possible to group commands) - You cannot specify a second command to be run If the condition is not met The if Statement -A much more powerful (and readable) shell programming construct is the if stat
Posts
Showing posts from June, 2010
bash | read variable
- Get link
- X
- Other Apps
By
zr0
-
Many shell variables are inherited from the login shell Environment. In other words, they are preset variables For example, when running a script the following Variables will be available (amongst others): -HOME -PATH -LOGNAME -TERM echo $LOGNAME --> iam user Such variables may be changed by the script, but the changes will not be seen by the login shell unless the script was run using the "." operator. llist=`who | sort` #shell command echo $llist llist=who | sort #string echo the date is `date +%m/%d/%y` today Groupping commands: ( ls date ) Please enter your name: ; read name Tom and Jerry echo $name Tom and Jerry Please enter your name: $name This command is split \ over several lines ------- Conditional code True and False -When every UNIX command completes, it invisibly Returns a value to the program that started it (usually the shell) Informing that program of the "status" of completiono f the command - This
run bash | set variables
- Get link
- X
- Other Apps
By
zr0
-
If it ever becomes necessary to pass one of the special Characters as a parameter to another program, one of three Actions is required: -prefix the character with a \ (for example, \$) -surround the character with a pair of " (for example "#") Note, this doesn't work for all characters - surround the character with a pair of ' characters (for example '$') This works for all characters except ' echo "the fee is $10" --> the fee is 0 echo 'the fee is $10' --> the fee is $10 echo the fee is \$10 --> the fee is $10 echo the fee is "$"10 --> the fee is $10 echo the fee is "$10" --> the fee is 0 There are 4 ways to run a shell script on the command-line 1.myscript The script must be executable 2.sh myscript Technically the same as method 1., but the file doesn't need To be made executable 3. .myscript The commands in the script are executed by the CURRENT shell 4.exec m
- Get link
- X
- Other Apps
By
zr0
-
echo enter your name: read name Mark V echo hello $echo read number stree type 12 Smith Road echo $number -->12 echo $street -->Smith echo $type --> Road If more words are provided than there are variables, the Extra words are added to the last variable If not enough words are provided, the extra variables will Contain nothing cat f echo -n "Enter your name:" read name echo Hello, $name Позволяет вводить значение переменной в той же строчке cat f echo "Enter your name:\с" read name echo Hello, $name \с такой же результат что и -n -- Standard Error is a separate stream from Standard Output, And mey be redirected to a file using 2> For example: ls a f ls a f d > filelist 2> errors cat errors Невозможно получить достук к d -- Standard Output and Standard Error can be directed to the same file as follows: ls a f d > filelist 2>&1 -- Any unneeded output may be redirected to the special file /d
- Get link
- X
- Other Apps
By
zr0
-
#mv folder newfolder Если папка newfolder уже существует, папка folder Копируется в папку newfolder. Если папки newfolder нет, folder переименовывается в Папку newfolder man -k = apropos nohup command & - выполнение команды будет продолжено после выхода из системы Весь вывод команды сохраняется в файле nohup.out Cmp Diff Echo * == ls rename 's/\.bak$//' *.bak - strips .bak from the filename Экземпляр выполняющейся программы - процесс Например, wc - это программа; каждый раз когда она запускается, создается новый процесс (со своим PID) ps -ax (BSD) = ps -ef (System V) ps -ag - показывает информацию о процессах, Исполняющихся в этот момент. export - позволяет использовать переменные в разных программах Byte= symbol in unix file - команда делает предположение о типе файла crypt - используется для шифрования файлов от root'a newgrop - изменяет текущую группу пользователей /etc/passwd - регистрационный файл /bin/passwd - программа дл