john pfeiffer
  • Home
  • Categories
  • Tags
  • Archives

Bash pipe redirect sudo tee append double pipe true shell command line math zero truncate file begins with dash

[TOC]

Controlling exit codes and interrupts

./some-command.sh || true # continues no matter what, ignoring if an error is returned by the program ./some-command || exit 1 # if the first returns an error the second (exit) will execute, otherwise if all good the second is ignored


Tips on Sudo

you cannot sudo a > , tee accepts stdin and displays AND writes to a file echo "deb http://us-east-1.ec2.archive.ubuntu.com/ubuntu/ precise main" | sudo tee -a /etc/apt/sources.list

alternatively you can try (i.e. packer so extra escapes for the quotes) "sudo su -c \"echo '# foobar test' >> /etc/apt/sources.list\"",


date > date.txt # write the output of the date command to date.txt (overwrite if exists) date >> date.txt # append to the end of date.txt (create if not exists) cat date.txt | grep 53 # display the contents of date.txt piped to grep which only displays lines containing 53 grep 53 <<< cat date.txt # equivalent statement where stdin of grep comes from displyed contents of date.txt

find . -name ".bak" -type f -print | xargs /bin/rm -f # xargs are safer (spaces in arguments) than backticks ` or $() shells find . -name ".bak" -print0 | xargs -0 -I {} mv {} ~/old.files


sed 's|http://||' <<EOL # redirect stdin so that EOL terminates the input, sed removes the http:// http://url1.com http://url2.com http://url3.com EOL

prints url1.com url2.com url3.com


./my_script > file <-- Redirect standard output ./my_script 2> file <-- Redirect error output ./my_script &> file <-- Redirect all output

command >> /tmp/log.log 2>&1

The 2>&1 says make STDERR point to the same places as STDOUT

STDOUT is open already, and the shell has done a seek to the end, STDERR will also be appended to STDOUT.


:> filename (truncates the file to 0)

cp /dev/null /log/file


if the file name begins with a -

cp -- /dev/nul -somefile # the -- means end of options :> ./-somefile # dot slash prefix indicates the current directory


note for fractions/decimals you need "bc"

echo $((1+2))

$((expression))

id++ id-- variable post-increment and post-decrement ++id --id variable pre-increment and pre-decrement - + unary minus and plus ! ~ logical and bitwise negation * exponentiation * / % multiplication, division, remainder + - addition, subtraction <> left and right bitwise shifts = comparison == != equality and inequality & bitwise AND ^ bitwise exclusive OR | bitwise OR && logical AND || logical OR expr?expr:expr conditional operator = = /= %= += -= <>= &= ^= |= assignment expr1 , expr2 comma


  • « Virtualbox commandline headless summary install extpack import ova
  • unittest data driven dynamic test cases »

Published

Aug 9, 2015

Category

linux

~360 words

Tags

  • append 1
  • bash 7
  • begins 1
  • command 29
  • dash 1
  • double 2
  • file 92
  • line 31
  • linux 249
  • math 2
  • pipe 4
  • redirect 8
  • shell 2
  • sudo 2
  • tee 1
  • true 1
  • truncate 2
  • with 29
  • zero 1