Creating user defined commands in Linux.

Status
Not open for further replies.

nitinriaan

New Member
Joined
May 30, 2022
Messages
2
Reaction score
0
Credits
32
Steps for creating user-defined commands:-

1. Enter the command “sudo -i” //which will log in to root

2. Enter the password: now it will be logged n to the root

3. Change the directory to bin type “cd bin”

4. Using the nano command create a file that should be the name of the user-defined command “nano commandName”

5. Write the script that you want to execute and save it

6. Change the permission of the created file to make it executable to all users “chmod +x fileName”

7. Now you log out from the root by typing the exit command.

8. Now run the created command it will run if any error occurs repeat the steps.

We created 3 user-defined commands they are as follows:

i. cat1

ii. mydate

iii. desktop


i. cat1

code:
echo "contents of $1 as follows:"
cat $1
echo "------END------"

ii. mydate
code:
echo "Today's date:"
date +"%d-%m-%y"
echo "Day:"; date +%A
echo "Month:"; date +%B
echo "-----DONE-----"
exec bash




iii.desktop
code:
echo "Welcome to Desktop"
cd ~/Desktop
exec bash
 


I'm guessing this is for system-wide scripts, since you're putting them into /bin (or /usr/bin).
- Script-writing best practice is to put #!/bin/bash as the first line, to identify the target shell.
- It isn't advisable to sudo to root, because it opens the filesystem to potential accidents. Better to sudo nano /bin/scriptname.
 
I wouldn't get too attached. They use phrases like "We created ..." and that doesn't bode well for this account. There's some other information available to some of us that tells us this is almost certainly a spam account.

What they'll do is come back later to add a URL to their post, or comment on their post with another account and leave a URL in that comment. We've had a bunch of them lately. I'd love to be wrong, but I am probably not.
 
The better practice is to place user created scripts used as commands in /usr/local/bin, in /bin -> usr/bin binaries are usually installed by packages installed through your package manager.
 
Last edited:
The better practice is to place user created scripts used as commands in /usr/local bin, in /bin -> usr/bin binaries are usually installed by packages installed through your package manager.
I agree!
Use /usr/local/bin/ for system-wide scripts.
Or if the scripts are just for yourself, put them in a personal bin sub-directory in your home directory.
E.g. in ~/bin/ or ~/.local/bin/
 
Status
Not open for further replies.

Staff online

Members online


Top