run c++ program in linux terminal with command - linux

I have a cpp file called FileSystem.cpp, while I want to use the linux terminal and call the FileSystem executable file with command
FileSystem -i
" no matter where it located and call it without extension or './' at the front. I tried call it directly from terminal but it said:
FileSystem: command not found

When you type a command into the command line like FileSystem -i, without an explicit path on the command (no / characters in the first word), it looks for the exectuable in your $PATH. You can use the command echo $PATH to see what your current path is.
Normally, on linux, your path will include the directory $HOME/bin if it exists. That's the bin directory in your home directory, so you can put an executable you create (such as FileSystem) in that directory and then run it as FileSystem -i

Related

How do I move files out of a broken directory in linux?

I know the premise of the question may be confusing, but I want to understand what happened.
Recently I have been experimenting with the rockchip OK3399 single-chip computer(see here) and have installed a linux system on it with TF card installation. Using Putty and connecting with serial protocol, I was able to establish a connection with the OK3399 computer and control it through my laptop.
I am trying to self-learn some linux with the OK3399 system. I created a bash code by the name of displayvids.sh inside the directory /usr/bin, which is meant to take a variable number of pictures with a mipi camera and then save in a directory for work.
I finished writing the code, but for some reason I cannot run the .sh file when my working directory is not the /usr/bin directory, despite /usr/bin being in the %PATH% environment variable. So, I executed the following command:
mv /usr/bin/display* /usr/local/bin
... attempting to move the file to /usr/local/bin instead. The command ran successfully, but when I tried to run the command:
cd /usr/local/bin
It tells me that I cannot cd to bin
As seen from the above image, the /usr/local/bin is not even a directory. Why would mv succeed if the target was not a directory? How can I retrieve my bash file?
Why would mv succeed if the target was not a directory?
mv can also rename files:
mv foo.txt bar.txt
You renamed your script to bin and moved it under /usr/local.
You may want to remember to add a trailing slash next time, to have mv barf if the target isn't a directory:
mv /usr/bin/display* /usr/local/bin/
How can I retrieve my bash file?
Rename it back.
mv bin displayvids.sh
For future reference, you can use the file command to (try to) identify the contents of a file, if it's installed:
file bin
would have probably said bin: Bash script or similar.

Create executable file for comand line Linux

When ever I log into Linux I usually go straight to the same folder i was wondering if in stead of typing in:
$cd Document/..../..../..../..../....
I could create an executable so I could just type ./csFolder and it would go straight there.
You can add a shell function in your .bashrc and restart your terminal:
csf() {
cd Document/..../..../..../..../....
}
Whenever you want to go to that directory, you just run csf.
Yo can do a symlink
ln -s /path/to/file /path/to/symlink
In addition to the other options (though if you use the function/alias option you want to use an absolute path to the target directory so it works from wherever you happen to be) you can use the environment variable CDPATH to help with this if you have a location you often go to from various other locations.
From the POSIX specification:
CDPATH
A -separated list of pathnames that refer to directories. The cd utility shall use this list in its attempt to change the directory, as described in the DESCRIPTION. An empty string in place of a directory pathname represents the current directory. If CDPATH is not set, it shall be treated as if it were an empty string.
Which means that if you set CDPATH to the parent of your target directory you can just use cd dirname from anywhere and go directly to the directory you wanted to be in.

Cygwin /cygdrive/c no such file or directory

Host system: Windows Server 2008 32-bit
Installed: Cygwin
I don't know when this problem started, but one of my Rails gems uses the command which to determine the location of a system-installed executable. In my circumstance, it returns /cygdrive/c/Windows/System32/pngcrush - and the file is inaccessible.
In both cygwin terminal and Windows cmd I get the following:
ls -la /cygdrive/c = No such file or directory
mount =
C:/cygwin/bin on /usr/bin type ntfs (binary,auto)
C:/cygwin/lib on /usr/lib type ntfs (binary,auto)
C:/cygwin on / type ntfs (binary,auto)
B: on /cygdrive/b type smbfs (binary,posix=0,user,noumount,auto
C: on /cygdrive/c type ntfs (binary,posix=0,user,noumount,auto)
Running cd / in Windows terminal brings me to C:\
cd /cygdrive = The system cannot find the path specified
FURTHER EDIT:
I can access the drives using /c for example, but just not /cygdrive/c which is what which returns.
I found the answer on superuser.com; moving C:\cygwin\bin to the front of the PATH variable fixed the issue.
This fix was necessary, but not sufficient, for me; one more fix was needed. I was getting the error running Bourne shell scripts which referred to other files on the localFS. All the paths checked out. It wasn't until I used Sys Internals procmon to troubleshoot that I noticed an extra char at the end of the path that wasn't resolving. Viewing the shell script in Notepad++ with View/Show Symbol/Show All Characters revealed Windows style [CR][LF] line ends, not Unix [LF]. Cygwin's sh.exe was including the [CR] on the end of the path, resulting in file not found. I appied cygwin's d2u to convert line ends, and the problem was fixed.

Running files in Linux terminal

On a Mac one can run any downloaded executable in the terminal using just the executable name. On Linux, such as Ubuntu, by default one will have to specify the directory to run in.
Mac example: sbt
Linux example: ./sbt
What do I need to set in Linux so that I don't need the ./ in front of the executable?
This should work:
PATH=$PATH:.
or in bash:
PATH+=:.
You can define an alias in your .bashrc file if it is an often used file. Or just add the directory in which your files is located to your $PATH var.
Copy your script in /usr/local/bin

How to create my own command in linux

I wrote a simple cpp code by using vi. I want to use cat command inside this program (by using execl and it will take my file as argument). I also have to write my own command. For example it will work like that: $mycommand filename. And cat command which is inside my cpp code will show my cpp file's content. I'm new so I don't know how to do it. Do you have any suggestions?
1) Redirecting output
The output of the cat command should go to the standard output of your program.
For this your program should pass its standard output FD to the exec command. This will be the default case. So you don't have to do anything extra.
2) cat on the source file
Now, the cat thing : "And cat command which is inside my cpp code will show my cpp file's content. " This statement does not explain your need. If you want the cat of the executable you are running, you can do an objdump on your executable. The executable path can be found out from the proc file system with your pid. For a generic case, it does not matter whether the file is a cpp file or not.
3) making command
To make your executable run as a standard command on a terminal, you must add the directory containing the executable to the PATH directories list. You can do so by adding your executable to /usr/local/bin or /usr/bin. (Convention is that it should go in /usr/local/bin since it has been compiled locally). If you don't have administrator privileges on the machine, you must append the source directory to the PATH directory list.

Resources