Created
April 28, 2021 16:12
-
-
Save mohamad-wael/5f292cfb589f8f10395fe79dc5d74fa3 to your computer and use it in GitHub Desktop.
shell for loop example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Redirecting the output of a for | |
# loop to a file . | |
$ for x in {1..10} ; do echo $x ; done > filesave | |
$ cat filesave | |
1 | |
2 | |
3 | |
4 | |
5 | |
6 | |
7 | |
8 | |
9 | |
10 | |
# Piping the output of a for | |
# loop to another program | |
$ for var in `date` | |
> do | |
> echo $var | |
> done | tr '\n' ' ' | |
Wed Apr 28 17:43:16 EEST 2021 | |
# Execute the date command , | |
# its output is | |
# Wed Apr 28 17:43:16 EEST 2021 | |
# Whitespace , new lines and | |
# tabs are considered word | |
# separators , depending | |
# on the value of the shell | |
# IFS variable . IFS stands for | |
# Input Field Separator . | |
# echo will echo back each word , | |
# so the output from the for loop | |
# should be like this : | |
# Wed | |
# Apr | |
# ... | |
# Use the translate command , to | |
# translate new lines to a white | |
# space , so the final output | |
# of executing the command is | |
# Wed Apr 28 17:43:16 EEST 2 | |
# Perform a long computation in the background . | |
# The result of the computation is saved into | |
# the file save_result | |
$ for i in {1..1000000}; do echo i; done >save_result & |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment