Sharing my new script - making symbolic links

About writing shell scripts and making the most of your shell
Forum rules
Before you post please read how to get help
Post Reply
edcompsci
Level 2
Level 2
Posts: 62
Joined: Fri Dec 21, 2012 5:25 pm

Sharing my new script - making symbolic links

Post by edcompsci »

Every time I use a script I find online I think, "Well what can I share back?"

This one simply prompts the user to put symbolic links into a folder for use as wallpapers, to take advantage of the randomly changing backgrounds option in Desktop Settings.

I hope others enjoy it. Not a huge learning curve.

Code: Select all

#!/bin/bash

# uses arithmetic... echo "$(($a + 1))"
clear

linkfolder='/path/to/folder/' 
echo "enter the number of the last symbolic link in the $linkfolder folder"
read lastnum
echo "The number you entered is $lastnum"
i="$(($lastnum + 1))"
echo "The next symbolic link will be number $i"

echo "How many wallpaper symbolic links do you want to make?"
read numlinks
echo "You entered $numlinks"
j="$(($i + $numlinks))"
echo "The number of the last link you make will be $(($j))"


####################################### loop ###########################################
while [ $i -lt $j ]
do
echo "enter the path to image $i"
read path2image
echo "making symbolic link..."
ln -s "$path2image" "$linkfolder/$i"
echo "Symbolic link made.  Verify if tihs is the first time running this script."
((++i))
done
#################################### end loop #######################################
rene
Level 17
Level 17
Posts: 7552
Joined: Sun Mar 27, 2016 6:58 pm

Re: Sharing my new script - making symbolic links

Post by rene »

Thank you; very useful for new scripters. One remark: you can in arithmetic expansion $((..)) leave out the $ for variables such as lastnum.
User avatar
Termy
Level 7
Level 7
Posts: 1621
Joined: Mon Sep 04, 2017 8:49 pm
Location: UK
Contact:

Re: Sharing my new script - making symbolic links

Post by Termy »

Please properly indent your code; you and any readers of your code will thank me later. ;)

Example of indenting a statement and a loop:

Code: Select all

if CONDITION; then
	COMMANDS
fi

while CONDITION; do
	COMMANDS
done
Whether you use tabs or any number of whitespaces, try to be consistent.
rene wrote:
Mon May 17, 2021 8:51 am
Thank you; very useful for new scripters. One remark: you can in arithmetic expansion $((..)) leave out the $ for variables such as lastnum.
Fun fact: there are other times you don't need the sigil ($), such as a subscript ${Here[is_a_variable]} and a substring ${Like:These:Variables}.
I use Linux Mint 18.3 with Cinnamon in a VirtualBox VM for testing & sandboxing.

I'm Terminalforlife (TFL/LL) on YouTube: https://www.youtube.com/c/terminalforlife
I'm also terminalforlife (TFL) on GitHub: https://github.com/terminalforlife
edcompsci
Level 2
Level 2
Posts: 62
Joined: Fri Dec 21, 2012 5:25 pm

Re: Sharing my new script - making symbolic links

Post by edcompsci »

Thanks for the replies/tips
Post Reply