Last active
March 4, 2022 18:14
-
-
Save d33pcode/2c5fc70dfddf6aa37b23 to your computer and use it in GitHub Desktop.
Bash snippets
This file contains hidden or 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
| #If the file doesn't exist, create the folder and the file in it. | |
| if [ ! -f "$FILE" ]; then | |
| mkdir -p $FOLDER | |
| touch "$FOLDER/$FILE" | |
| fi |
This file contains hidden or 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
| H=$(date +%H) | |
| if (( 5 <= 10#$H && 10#$H < 12 )); then | |
| echo "Good morning, $USER." | |
| elif (( 12 <= 10#$H && 10#$H < 17 )); then | |
| echo "Good afternoon, $USER." | |
| else | |
| echo "Good evening, $USER." | |
| fi |
This file contains hidden or 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
| titles="1) Use Terminal \n2) Upgrade packages \n3) Check mail \n4) Browse the net \n5) Open the IM \n6) Study \n7) Watch a movie \n8) Open TOR \n9) Open Telegram" | |
| options=("Use Terminal" "Upgrade packages" "Check mail" "Browse the net" "Open Finch IM" "Study" "Watch a movie" "Open TOR" "Open Telegram") | |
| select opt in "${options[@]}" | |
| do | |
| case $opt in | |
| "Upgrade packages") | |
| echo "Ok, wait." | |
| sleep 0.5 | |
| bash ~/Script/update.sh | |
| clear | |
| echo -e $titles | |
| ;; | |
| "Watch a movie") | |
| bash ~/Script/movie.sh | |
| clear | |
| echo -e $titles | |
| ;; | |
| "Check mail") | |
| bash ~/Script/mail.sh | |
| clear | |
| echo -e $titles | |
| ;; | |
| "Use Terminal") | |
| echo "Bye!" | |
| sleep 0.5 | |
| gnome-terminal | |
| exit | |
| ;; | |
| "Browse the net") | |
| bash ~/Script/elinks.sh | |
| clear | |
| echo -e $titles | |
| ;; | |
| "Open Finch IM") | |
| bash ~/Script/finch.sh | |
| clear | |
| echo -e $titles | |
| ;; | |
| "Study") | |
| bash ~/Script/study.sh | |
| clear | |
| echo -e $titles | |
| ;; | |
| "Open TOR") | |
| bash ~/Script/tor.sh | |
| clear | |
| echo -e $titles | |
| ;; | |
| "Open Telegram") | |
| bash ~/Script/telegram.sh | |
| clear | |
| echo -e $titles | |
| ;; | |
| *) echo actually this is not an option...please write a number from the list.;; | |
| esac | |
| done |
This file contains hidden or 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
| sed -n '7p' file |
This file contains hidden or 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
| ``` | |
| Use \r to go back to the beginning of the line without writing \n to the terminal. | |
| Write \n when you're done to advance the line. | |
| Use echo -ne to: | |
| not print \n and | |
| to recognize escape sequences like \r. | |
| Here's a demo: | |
| ``` | |
| echo -ne '##### (33%)\r' | |
| sleep 1 | |
| echo -ne '############# (66%)\r' | |
| sleep 1 | |
| echo -ne '####################### (100%)\r' | |
| echo -ne '\n' |
This file contains hidden or 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
| write_to_file () { | |
| # initialize local configuration file | |
| LOCAL_FOLDER="/path/to/dir" | |
| FILE="file.txt" | |
| # enter a while loop in order to write into the file | |
| while true; do | |
| read -p " " INPUT | |
| if [ "$INPUT" == ":Q:" ]; then | |
| # Quit the editor | |
| echo " All the lines have been written on your FILE" | |
| return | |
| else | |
| # push INPUT to FILE | |
| echo "$INPUT" >> "$LOCAL_FOLDER/$FILE" | |
| fi | |
| done | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment