DEV Community

technonotes-hacker
technonotes-hacker

Posted on

Shell >> Redirections >> Capture

  • Redirection meanings What ? Changing something.
  • Instead of putting the output to the screen instead redirect to the file.
  • ">" redirect and over write
  • ">>" will always append.
#!/bin/bash
read -p "Enter ur name: " name
echo " Welcome to BOMBAY $name !!"
Enter fullscreen mode Exit fullscreen mode

Image description

Also , you can choose which line needs to , go to the file.

#!/bin/bash
read -p "Enter ur name: " name 
echo " Welcome to BOMBAY $name !!" > log.txt
echo " Vanakam THozha " >> log.txt
echo " -- EOL -- "
Enter fullscreen mode Exit fullscreen mode

Image description

/dev/null

  • it will make it as NULL
  • it will work only for the regular consoles.

1 --> console output ( By default it works only for standard console output ) eg., > or >>
2 --> Error

nohup

  • script will run in background.
  • the process will run behind us and it won't show in the console.
  • it creates nohup.out
  • & --> run it in the background.
#!/bin/bash
num=1
while [0]
do 
    echo "Number is $num"
    num = `expr $num + 1`
done
Enter fullscreen mode Exit fullscreen mode

nohup file.sh 2>&1 &

Capturing

  • back ticks var = ls | wc -l ``or
  • $(command) var = $( ls | wc -l )

Both will return the value.

  • One problem with backtick is " it wont support nested parameters ".

Image description

so use $()

Image description

Top comments (0)