Take action or execute a command based upon shell script name

You would like to run different commands or task based upon a single shell script name. For example script name is backup Instead of writing 3 different script write one script and softlink using ln command:

=> backup – original script

=> backup.nas – Script to backup data to NAS device

=> backup.ftp – Script to backup data to FTP Server

=> backup.scp – Script to backup data to Remote SSH server

Use ln command to make link between backup and other files
$ ln -s backup backup.nas
$ ln -s backup backup.ftp
$ ln -s backup backup.scp

Now use basename command strip directory and suffix from script name. $0 variable always print scriptname.
#!/bin/bash
echo My name is $(basename $0)

Output:

My name is backup

Let us store output to variable
#!/bin/bash
ME=$(basename $0)

Now simply use the case statement to get script name. The case statement allows you to match several values against $ME variable. Here is final script:

#!/bin/bash
ME=$(basename $0)
case $ME in
        backup.nas)
           echo "Write code to mount NAS using smbmount, use cp to copy data to NAS"
           ;;
        backup.ftp)
           echo "Write code to copy data using FTP"
           ;;
        backup.scp)
           echo "Write code to copy data using OpenSSH scp command"
           ;;
        *)
           echo "Syntax error - show some help"
           echo "Use $ME.nas or $ME.ftp or $ME.ftp"
           ;;
esac

To backup data to NAS just type
backup.nas

To backup data to FTP server just type
backup.ftp

Benefits

  • Ease of use
  • Saves time
  • Your work looks quite professional

See also:

🐧 If you liked this page, please support my work on Patreon or with a donation.
🐧 Get the latest tutorials on SysAdmin, Linux/Unix, Open Source & DevOps topics via:
CategoryList of Unix and Linux commands
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
2 comments… add one
  • tASK Jul 20, 2007 @ 12:42

    Did you set binary mode before transfer file on FTP server?

  • Mahesh May 4, 2007 @ 7:05

    Hi

    I am taking tar.gz backup on RH9 and copying the file to windows ftp server shceduled shell script. I am seeing the file size difference at both location. Windows server is with ntfs partition. What could be the reason , that i am seeing 4 to 5 mb increased file size on windows server that RH 9 linux server?

    Regards /mahesh

Leave a Reply

Your email address will not be published. Required fields are marked *

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.