Skip to content

Instantly share code, notes, and snippets.

@totetmatt
Last active Jun 20, 2020
Embed
What would you like to do?
kata-starwars.sh
## Generation of files ##
cat > movies.txt <<EOL
A New Hope (1977)
The Empire Strikes Back (1980)
Return of the Jedi (1983)
The Phantom Menace (1999)
Attack of the Clones (2002)
Revenge of the Sith (2005)
The Force Awakens (2015)
Rogue One: A Star Wars Story (2016)
The Last Jedi (2017)
Solo: A Star Wars Story (2018)
EOL
cat > order.txt <<EOL
4
5
6
10
8
1
2
3
7
9
EOL
cat > expect.txt <<EOL
The Phantom Menace (1999)
Attack of the Clones (2002)
Revenge of the Sith (2005)
Solo: A Star Wars Story (2018)
Rogue One: A Star Wars Story (2016)
A New Hope (1977)
The Empire Strikes Back (1980)
Return of the Jedi (1983)
The Force Awakens (2015)
The Last Jedi (2017)
EOL
## Working Example ##
diff <(cat order.txt | xargs -I % sed -n "%p" movies.txt | tee result.txt) expect.txt || echo "No ok"
# diff : Make a diff between *files*
# <(someComand) : Use the stdout of the file "as a file"
# xargs -I % someCommand : Pass each line of the stdout form the previous pipe as a parameter, here the parameter should replace '%' on the someCommand
# sed -n "%{p;q}" movies.txt : Pick the '%' line from the 'movies.txt' file
# tee result.txt : Append the stdout to the file 'result.txt' but still print it
# something || somethingElse : If `something` doesn't return 0 (like $?!=0) do somethingElse
## Example if error (test failure)##
diff <(cat order.txt | xargs -I % sed -n "%p" movies.txt | shuf) expect.txt || echo "No ok"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment