User Tools

Site Tools


for

If you want to send us your comments, please do so. Thanks
More on comments


for examples

bash

for i in {1,3,6,8,9}; do echo $i; done
for i in {5..25}; do echo $i; done
for i in {5..25}; do echo -n "$i "; done; echo
for i in {0..30}; do echo -en "$i\t$(date '+%H:%M')\t"; whois example.com; sleep 1h; done
for (( i=1; $i<=5; i=$i+1 )) ; do echo -e "$i\n" ; done
for i in $(cat somefile.txt); do echo $i; done | less
for i in $(ls -1) ; do aplay $i; done # play the wav files in the direcory
for ((i=1;$i<=5;i=$i+1)){ echo -n "$i "; }; done
for file in .; do diff -s "$file" "/mnt/usbstick/$file"; done
for file in ./*; do md5sum "$file" "/mnt/usbstick/$file"; done (2.)
A=3; B=7; for i in $(eval echo "{$A..$B}"); do echo "Do $i with a dynamic range"; done


/tmp/ffilename.txt consistes of lines starting and ending with a

for i in "`head -n 4 /tmp/ffilename.txt`"; do echo -e "$i\n"; done | less -S

If this works you can replace head -n 4 with cat and echo -e “$i\n” with a command you want to preform on each line in /tmp/ffilename.txt

Variable in string

This

for i in {01..03}; do echo "Hello$iHello"; done

does not what you would expect it to do. The output is:

Hello
Hello
Hello

Solution:

for i in {01..03}; do echo "Hello$(echo $i)Hello"; done

Output:

Hello01Hello
Hello02Hello
Hello03Hello

Convert image files

Convert all *.BMP files to jpg files with extension jpg.

for i in *.BMP ; do convert "$i" "${i%.*}.jpg" ; done

i%.* removes the .* , .BMP , in this case
Because .jpg is added the .BMP extension will be replaced with the .jpg extension

Notices

  1. for statements use
    <=

    like comparison tokens. If statements needs things like

    -le

    for the same purpose

  2. Here ./* is needed to make it work. Why in this is not the case in the diff example . . . . .
for i in `cat differ.txt`; do ls -ls $i; done 2> error2.txt | less -S

differ contains the edited output of a diff -irq command and is a list of files with the full path. Two files per line like in

 /home/user/.config/geany/geany.conf   /var/backup/user/.config/geany/geany.conf

When such a line contains spaces in the filename like in

/home/user/.config/chromium/Default/Visited Links
/home/user/.config/chromium/Default/Visited\ Links
"/home/user/.config/chromium/Default/Visited Links"

the for statment gives an error. It sees a space as the start of a new filename. As you can see the of \ do not solve this

awk

test.for.awk

#!/usr/bin/awk -f
 
BEGIN {
for ( i = 1; i <= 10; i++ ) {
	printf ("Counter i: %d\n" , i)
	}
}
 
{
}
 
END {
}

Test it with

./test.for.awk test.for.awk

Calculate sum of filesizes

Work in progress: integration of first and second command and addition of bc for the calculation
Calculate the sum of the size of files greater or equal then 1.000.000 bytes

for i in {$(find ~ -size +10000000c -exec ls -l '{}' \;)}; do echo $i; done > count.txt
for i in $(cat count.txt); do if [[ $i -ge 1000000 ]]; then echo "$i+"; fi; done

Main subjects on this wiki: Linux, Debian, HTML, Microcontrollers, Privacy

RSS
Disclaimer
Privacy statement
Bugs statement
Cookies
Copyright © : 2014 - 2024 Webevaluation.nl and the authors
Changes reserved.

This website uses cookies. By using the website, you agree with storing cookies on your computer. Also you acknowledge that you have read and understand our Privacy Policy. If you do not agree leave the website.More information about cookies
for.txt · Last modified: 15-05-2023 16:56 by wim