How to create a custom bash command in linux - linux

The question is about bash shell commands in ubuntu 10.04.
I have created a simple addition program in c and it works fine in my terminal.
Now I want to make this program to execute into my terminal as a command.
How can I convert a C program into a bash shell command?
How to make that command an system command like others?

yout just have to change it's owner & group root following commands
sudo chown root "file_name"
sudo chgrp root "file_name"
then give this command to change the permissions
sudo chmod 755 "file_name"
and place it in /bin with this command
sudo mv "file_name" /bin
now u can run it as a normal command.

You run your code by ./compiled-c-program
If you like to run like the other "system" program you need to add a static link to your program to one of the folder from your $PATH variable e.g.:
ln -s ~/bin/c-compiled-c-program path/to/the/program/compiled-c-program
Good luck!

I guess you want this C program to be executed by any user, as a system command. If this is your requirement, then you can add an execute permission to everyone by chmod +x <program name> and then add the program absolute path in the system define PATH environment variable.

Related

How to give permissions for specific commands in linux

I am new to linux. I have a build.sh file which consists of a lot of mkdir commands and some rm commands. But as I have installed this new in my VB, each time I run the .sh file, it says "Permission Denied for creating directory" and fails.
So is there any way that I grant directory privileges to all users.
Can anyone help me with this
Add "sudo" in the beginning of the directory creation command i.e
sudo mkdir dir_name
The issue might be with the directory in which the mkdir command is being run.
Use the command ll or ls -l to check the directory permissions.
If your directory doesn't have write privilege for the current user, you can run
chmod -R u+w /path/to/directory
This might require you to use sudo if permission is denied.
If you want to enable it for all users, run
chmod -R ugo+w /path/to/directory
Alternatively, a quick fix would be to run the build.sh file as root
sudo /path/to/build.sh
However, this approach is not advised unless you always run it as root

Remove ".bash_aliases" with bash script

In my .bashrc I'm using .sh script for easily configuring newly installed Debian. But while trying to
rm -f ~/.bash_aliases
wget https://raw.githubusercontent.com/.../.bash_aliases
rm -f ~/.bashrc
wget https://raw.githubusercontent.com/.../.bashrc
it's just omitting those line?
File is with permission chmod +x ./script.sh and run by sudo ./script.sh
What could possibly be wrong?
(In final code there is full link, files are being downloaded as .bashrc.1 and .bash_aliases.1)
Don't use sudo unless you have a good reason.
When you run sudo ./script.sh it runs as root, so ~ refers to root's home directory /root instead of your user's home directory.
Just run ./script.sh instead, so that it runs as you and modifies your own home directory.

Execute script relative to another user's home directory

I'm a beginner in Linux scripting. Somehow I can't get the right search string to find the answer.
I want to run a script relative to a specific user's directory like:
~user/rel/path/to/script.sh
but I don't know what "~user" will translate to. It may even contain spaces. Should I quote it? And if so, how? I always get the file or folder does not exist error when I try to use quotes.
Edit
This is not a duplicate I think.
My concern was that running the following with quotes:
"~user/rel/path/to/script.sh"
gives me "file or folder not found" error. But I don't know, what ~user will translate to. (The script will be called on many different computers. The username is given but the home directory may be changed freely by the owner of each computer.) So I was afraid (as a Linux scripting BEGINNER!!!) to run it without quotes like:
~user/rel/path/to/script.sh
The most down-voted answer (Java system properties and environment variables) actually helped me most. I just needed to confirm that it works the same way on Linux. So I installed a test VM in VirtualBox and tried:
cd /
sudo mkdir -p "test home dir/myuser"
sudo adduser myuser
sudo chown myuser:myuser "test home dir/myuser"
sudo usermod -d "/test home dir/myuser" myuser
su myuser
cd ~
echo '#!/bin/bash -x
echo "here!"
' > test.sh
chmod +x test.sh
exit
cd /
~myuser/test.sh
And it worked!
On Mac OS you don't need to quote. I'm not sure about Linux. However, if
ls ~user
would result in /dir with space/user/ then
sh ~user/rel/path/to/script.sh
would be
sh /dir\ with\ space/user/rel/path/to/script.sh
and this executes if you have set the execution flag on script.sh accordingly.

How to run command in su mode in bash script?

I need to run these two commands :
ulimit -s 1024
echo 120000 > /proc/sys/kernel/threads-max
The first one can be run just in user mode (not using sudo or su) and the second can only be run in su mode. I want to write a bash script that let me run these two commands. The first one is OK. For the second one, I need to su (change user to root), run the command, and then exit. Actually, I want to run the second command in su mode using a bash script. Any idea?
If your user has permission to use "sudo tee", then one solution is:
echo 120000 | sudo tee /proc/sys/kernel/threads-max
As a security measure, you cannot run scripts as a superuser without prepending sudo. If you want it to be passwordless, you need to run visudo and allow your (or the executing user) to run this command as a superuser without password confirmation.
The other way is to use the setuid bit on compiled code. Compile a simple program which will execute the echo 120000 > /proc/..., then change it to be owned by root: chown 0:0 executable_name, and chmod u+s executable_name to set the setuid bit on it. This will cause execution of this program to be ran with permissions of its owner, which is root.
This is the same way which allows passwd to modify a file which requires super-user privileges without actually being a super-user or sudoer.

Running a file as nonroot from a root bash script

Okay, I currently use an eggdrop IRC bot on my server. I want to make a bash script to start it up as well as a few other scripts at the same time. All that's left is to make it start, but it won't run as root.
However, I also need to be in the current directory of the file to run it, or it displays an error.
For example:
/home/eggdrop/eggdropbot/eggdrop will display an error, so to run it I need to
cd /home/eggdrop/eggdropbot and then ./eggdrop
So I can't just use "sudo -u eggdrop /home/eggdrop/eggdropbot/eggdrop" and as you probably know, sudo won't cd, either, since that would be pointless.
Any suggestions?
Why not just cd first and then sudo -u ./eggdrop .?
What about doing the cd, and, only then, launch the command with sudo ?
I suppose something like this should do the trick :
cd /home/eggdrop/eggdropbot && sudo -u eggdrop ./eggdrop
You can cd to the directory as the root user and then use sudo -u to invoke the program from the working directory.

Resources