I'm trying to learn to write shell scripts and use the Terminal.
In Users/user/Development/linux I've got a script called sysinfo_page.
So I'm in the linux folder in the terminal and I can see the sysinfo_page when I type the ls command.
However, when I enter the following command:
sysinfo_page > sysinfo_page.html
I receive the following message:
-bash: sysinfo_page: command not found
How do I resolve this?
If you want to run a script file form the current directory, you have to write ./ before your script name:
./script.sh
Your command may not be an executable file. Try this:
chmod +x sysinfo_page
./sysinfo_page > sysinfo_page.html
The first line will set the eXecutable flag on the file, the second will run it from the current dir. Note that if you want to run a file in the current directory and that dir is not included in your PATH, you need to prepend ./ or else the shell won't find it.
Related
So I'm trying to write a script that will let me run a command to initialize some things. To be more specific, let's say I start in my home directory but to run this command I want I must be in a directory three folders deep into the home directory.
My script looks generically like this.
#!/bin/sh
cd home/path/to/final/directory/
command
Now usually, when I cd to this directory I can run the command on the command line and everything works fine.
When I tried to use a script to do it, the command line throws an error saying that that command isn't recognized like the computer doesn't know where to look.
The temporary fix I used was making a symbolic link to the directory I wanted but I was hoping someone could help me so that when I ssh to this node this script can be run immediately so I will not have to go into the deep directory, run the command and leave again.
Try defining full paths, for example:
#!/bin/sh
cd $HOME/path/to/final/directory && /path/to/your/command
In this case, it will try to cd into your defined directory but if it can't find the dir it will not run the command, this because of the &&
To test before running the command you could do a ls, for example:
cd $HOME/path/to/final/directory && ls
My problem is fairly simple.
I have a file that I want to run, say x.sh.
From my root location I run something like.
$ cd Desktop/./x.sh -args
It prompts me to go to the directory and then run the file.
And as prompted the following works
$ cd Desktop
$ ./x.sh -args
Now, Am I missing something here or is this just the way Ubuntu is designed to work?
You don't use cd while executing a file. Directly execute it on the prompt: Desktop/x.sh -args
You're missing something. You can either cd to the directory and run the file from that directory or you can run the file from the absolute path or relative path, provided the user you're trying to run it with has access to that directory. The following should work at the level you were trying to do cd Desktop/./x.sh -args:
./Desktop/x.sh -args
I have project structure like this
project
|app
|script
inside script folder, there are files such as 'run'
run file content:
#!/bin/bash
npm start
I want to run the file 'run' while I'm at the root of my project by typing only command 'run'. How would you do this?
This is sh file. In order to execute sh file on linux this file has to be executable.
Make sure this file has X permission.
If there is no x permission on file simply execute the command
chmod +x run.sh
Then execute the file by typing
./run.sh
For windows you need to create .bat file.
I'm not quite sure what you want but assuming you need a way to execute a file from node.js, you can use child_process module and child_process.exec method to start any executable.
Assuming the run file in the script directory is executable (if not, run chmod +x script/run), it can be executed by running ./script/run.
If you want to avoid having to type the name of the directory (script), you could append the script directory to your PATH environment variable. If you’re running a POSIX compatible shell (not csh or tcsh), this can be done using:
export PATH="$PATH:/path/to/project/script"
This will allow you to run any executable command in the script directory without having to specify the name of the directory, e.g., run.
NB: be sure that there aren’t common command names in the script directory as these commands can be run from any directory (including outside the project directory) after it has been added to the PATH. That’s also why I suggest adding it to the end of the PATH (so it’s the last directory that’s searched for executable commands).
I have a file called butcher and at the top of this file I put #!/bin/bash. The I changed permissions like so chmod 777 butcher. however when I try to run the script by typing butcher, I get this error: -bash: butcher: command not found. I'm not sure how to fix it, please help! Thank you in advance! :D
The file would have to be on your $PATH for your system to find it and execute it. Otherwise, the entire path to the file would have to be specified.
To show your path try:
echo $PATH
To run your script, either put it in one of those locations (such as /usr/local/bin), or go to the directory butcher is located in and run ./butcher.
When i login to the cygwin terminal and type:
cd "cygdrive/c/existing/path"
it tells me, "no such file or directory". i am sure the path exists... do i miss a special cygwin package, or do i have a false configuration? i am puzzled...
It behaves the same when i try to call the cygwin bash from a windows batch file.
what i basically want to do is creating a windows batch file which starts cygwin and executes a shell script with a specified working directory as its described in this blog post: http://blog.dotsmart.net/2011/01/27/executing-cygwin-bash-scripts-on-windows/
my batch file seems to work, it does the following command:
%_CYGBIN%\bash.exe --login "cd %_CYGPATH%" "./%_CYGSCRIPT%"
but cygwin won't execute the 'cd' command. The console output of my batch file is:
/usr/bin/bash: cd /cygdrive/c/existing/path: No such file or directory
cd '/cygdrive/c/existing/path'
# ^
# \
# --- need forward slash (/) before the "c"