===== Links =====
Terminals:
* [[habrahabr>164687|Альтернативный терминал для Windows]]
* [[habrahabr>176077|Интересная замена стандартного терминала: Terminology]]
* [[habrahabr>185944|Final Term: новый взгляд на терминал]]
* [[habrahabr>248881|Обзор Friendly interactive shell (fish) и почему она лучше bash]]
* [[habrahabr>204368|Linux-like терминал в Windows]]
* [[http://software.jessies.org/terminator/|Terminator]] -- a cross-platform (Java) GPL terminal emulator with advanced features
Shells:
* [[https://packages.debian.org/jessie/shells/|Shells in Debian]]
* [[http://fishshell.com/|Fish]] -- smart and user-friendly command line shell
* [[habrahabr>267797|Консоль 21 века: mosh, tmux, fish]]
Other tips:
* [[http://www.spinics.net/lists/util-linux-ng/msg13866.html|New ''columns'' syntax]]
===== Questions answered =====
=== How to use variable in while loop? ===
VAR=0
ls -1 | while read file
do
[ "$file" = "tmp" ] && VAR=1
done
echo $VAR # It turns out that $VAR is always zero here
*
echo "tmp.txt" | read file
VAR=0
while read file
do
[ "$file" = "tmp" ] && VAR=1
done < <(ls -1)
Process substitution which is used in above example, is described [[http://wiki.bash-hackers.org/syntax/expansion/proc_subst|here]]. Another good application of process substitution is [[stackoverflowa>43972501/267197|capturing the exit code of first command in a pipe]]:
if mount /dev/sda /mnt 2> >(head -1)
then
...
fi
The same way ''read'' and ''arrayread'' when applied after the pipe initialize the variable in a forked shell (hence ''echo file.txt | read line'' does not work) so the solution looks this:
read file < <(echo file.txt)
- From [[http://unix.stackexchange.com/questions/197121/|Create variables and assign values via loop]]:
VAR=0
for file in $(ls -1)
do
[ "$file" = "tmp" ] && VAR=1
done
read -r -d '' SCRIPT <<'EOS'
use strict;
my $lines = 0;
while (<>) {
chomp;
$files++;
}
print "Number of lines: $lines\n";
EOS
perl -e "$SCRIPT"
while IFS= read -r line
do
echo "$line"
done < some_file.txt
Similarly to read the whole file into variable:
IFS= FILE=`cat some_file`
(
echo -e "File\tMD5\tSize\tCTime"
find . -type f -printf '%p\t%s\t%T+\n' | sort -k1 -t$'\t' | while IFS= read -r line
do
echo -e "$(md5sum "`echo "$line" | cut -f1`" | cut -f1 -d' ')\t$line"; \
done
) | column -t -s $'\t'
See also:
* [[https://unix.stackexchange.com/questions/57222|How can I use column to delimit on tabs and not spaces?]], [[https://unix.stackexchange.com/questions/35369|How to define 'tab' delimiter for 'cut'?]]
* [[https://unix.stackexchange.com/questions/118433|Quoting within $(command substitution) in Bash]]
seq 1 10 | paste -s -d+ | bc
$ cat file | tr -d -c '#' | wc -c
if [[ $fullpath == *.txt ]]
then
echo "This is text file"
fi
file=`basename "$fullpath"`
extension=${file##*.}
filename=${file%.*}
array=( "${array[@]/#/prefix_}" )
array=( "${array[@]/%/_suffix}" )
To print an array as new-line separated string:
IFS=$'\n' echo "${array[*]}"
IFS=$'\n' echo "$*" | xargs ...
tr -dc A-Za-z0-9 < /dev/urandom | fold -w 16 | head -5
$ myprog
# press Ctrl+Z to suspend the program
$ bg
$ disown
# switch to another terminal / screen / tmux session
$ reptyr $(pgrep myprog)
set bell-style none
However bell can be useful to notify long running task:
* [[stackoverflowa>7895288/267197|Configure PuTTY to flash Windows taskbar on bell]]
* Latest ConEmu (v170807) supports this out-of-box unless //Disable all flashing// is not enabled in //[[https://conemu.github.io/en/SettingsFeatures.html|Settings → Features]]//.
stdbuf -oL tail -f /var/log/messages | tee logs.txt
Таким образом, оболочки sh и csh, а также tcsh при открытии нового файла, не используют флаг ''O_APPEND'', а описанный выше способ обнуления используемых файлов в этих оболочках использовать не получится. В более современных оболочках (ksh, bash, zsh) этой проблемы нет.{{tag>terminal shell}}