How to create a custom command in terminal? - bash

I need help creating a custom command in the linux terminal, for this I have the following problems, I want to do:
copy file path: /test /file
How can I insert a user "input" in bash format so that later the terminal understands the file I want to copy and the path?
Sorry if I don't explain myself very well ... I'm new ...

First of all, do not worry about being new. We are all here to learn and help each other!
Assumption
From what I understand, you would like to create a Bash script which accepts user input specifying both the filename and the destination path for the file to be copied to. The code below does exactly that but note that it does not have the auto-complete feature when accepting user input.
Solution
script.sh
#!/bin/bash
# ask user for file name to be copied
read -p "Name of the file to be copied:" filevar
# ask user where they want the file to be copied to
read -p "Destination path:" pathvar
# does the copying
cp -- "$filevar" "$pathvar"
Usage clarification
In case you are not sure how to run the script, I will give you an example scenario. Open your terminal and make sure you are in your Desktop directory. To run the script, enter ./script.sh. Then enter the file name to be copied, press Enter, and then enter the destination path. In your case, it is test (assuming you have a test folder on your Desktop). Press Enter one last time and the file will be copied to the destination.
Just let me know if you were expecting something different as I could not comment on your post to get more clarification due to my low reputation.

Related

Executing a bash script from anywhere on Windows

I am on Windows.
I have a script file named basics.sh and here is what it contains:
cd opt-out-exam/abduvosid_malikov/IT
mkdir made_by_my_script
cd made_by_my_script
echo "Hello World" > hello.txt
so basically, basics.sh script file is responsible to:
go to folder opt-out-exam/abduvosid_malikov/IT
make a directory made_by_my_script
create hello.txt file with content Hello World
Right now. to execute this basics.sh script, I am going to IT folder and writing this command in the terminal:
./basics.sh
In order to execute this basics.sh script, is it compulsory for me to go to IT folder
OR
is it possible to execute this script file even if I am staying in another folder (lets say currently working directory is opt-out-exam)
The first line is a change directory command followed by a relative path, not absolute. In such cases, it is important where you run the script. (An absolute path would start with the filesystem root, i. e. /.)
If you run this script from a directory (I wouldn't call it a folder in this context) where the relative path opt-out-exam/abduvosid_malikov/IT does not exist, it won't cd into it. But it will make a new directory without any problem, it will also create the file and write a line into it.
So only the first line will fail if it's run somewhere else.
UPD: As Gordon Davisson pointed out, this means that you want to check whether the directory change actually took place or not.

Using source command for other users logged in

My problem is about using the source command for everyone logged in.
Let's say I have a file with the content:
#!/bin/bash
alias ha='echo test'
Now if I source it and write ha, I'd get the message test.
Now what if there's someone else logged in, and I want it to be sourced for his account? Can you write something like source (username) (filename)?
Provided that other uses have read permission to your script, and read permissions to its directory and parent directories, they can source that file. Assuming your user name is "thesourcequestion", and you are keeping the alias file (let's call it alias ) in your home directory, other users can source it with the following command:
source ~thesourcequestion/alias
Or just (same thing):
. ~thesourcequestion/alias
Incidentally, some shells only accept the source keyword (like c-shell IIRC).
If you are the admin of the box, consider the solution presented here:
https://askubuntu.com/questions/610052/how-can-i-preset-aliases-for-all-users/986053
In short, there are system files that can be modified that will create the alias for users. The alias will be available to them next time they login.
Don't overlook the part of my answer about providing permissions to your file. For example, you can give read permissions to the file this way:
chmod a+r ~thesourcequestion/alias
The above syntax adds (+) the read attribute (r) to all (a).

How to go to a specific file in Mac Terminal?

I'm trying to follow a walkthrough and this is one of the steps, "go to that same folder in Terminal (if you can't do that you should probably quit now)" I'm not very familiar with Mac's Terminal, but don't feel like quitting. If it helps, I need to run a grep -r "what I'm looking for" command on the file. I really have no idea what I need to do to run that command, but the rest of the walkthrough is pretty thorough, so I know I can follow the next few steps.
You need to move to the directory where that file is stored, for this the command is cd. For example lets say the file is located in a directory names MyDir in the Desktop, the command will be
cd /Users/{YOUR_USERNAME_HERE}/Desktop/MyDir
You can run ls command here to check if the file is actually present in this directory or not

run shell script in one line from applescript and provide input

I'm trying to automate the installation of a program that needs to be run from the Terminal on MacOS. This seems simple enough but the installation script expects needs to run as root (or sudo) and find files from the root directory that it's running from and then looks for the user to provide a password as terminal input before it completes. I don't mind providing the password in a readable format as we will only be using it internally and the password will be changed soon after we're finished with this project.
I have the following so far and it's not working because it doesn't seem to accept my input from the response file. I'm using the sleep to wait the few moments that it takes to execute that first step.
do shell script "cd ~/Desktop/; ./setup; /bin/sleep 5; ~/Desktop/MyApp/Tools/RunThis_WithVarialbles -s blah.com -someOtherVaiable -u uName#blah.com < ~/Desktop/sccm/Tools/response.txt" password "passwdIDCisVisible`enter code here`" with administrator privileges
The error I get from Script Editor is:
error "/bin/sh: ./setup: No such file or directory
ERROR 2 when mkdir in CreateDirectory - No such file or directoryERROR 2 when mkdir in CreateDirectory - No such file or directory
Please enter your password.
(null)(null)(null)(null)(null)" number 8
I'll be the first to admit that I'm a total Noob! Thanks for your patience!
Ok, so I can see a couple of wrongs in your command already. And it still seems pretty unclear what you are trying to achieve. Yet, instead of asking a lot of questions and causing confusion, let's just stick to the first step.
The first part of your error message reads: error "/bin/sh: ./setup: No such file or directory. So this could indicate that:
~/Desktop/setup does not exist. (Check if it does).
~/Desktop/setup is not properly specified and you need to add a file extensions (e.g. like ~/Desktop/setup.sh)
You do not have the proper rights to execute the file. So you should execute something like chmod u+x to give execution rights to your current user. (And sudo chmod u+x for root).
See if you can get passed that first. Learning to code usually means taking baby steps at first.
Good luck.

Comparing variable with File names bash

I've recently started to learn bash script and have started to create a file repository system. I have gotten pretty far and am able to add files, remove files. When I remove a file from the repository it actually leaves the file in but changes permissions so only the user that removed can use it, it then send a copy to there home area, it also changes the name of the file left behind to "$fileNameOUT"
I know plan to add a feature to my add function which checks after a file has been added if there is a file with the same name but with "OUT" at the end, if it finds this the old file will be sent to a back folder so files can be restored. I know I have to loop through the directory using a for loop, however the problem I'm having is I don't know how I can compare the file I have just added to all of the files in the directory.
I hope someone can make send of what I just wrote.
If you know the name of the file you are interested in, you can use the -e test to check if it exists.
if [ -e fooOUT ]
then
echo File exists
fi

Resources