I have written a Unix command to copy a file from one location to other and saved it with script.sh in my E:\ drive in Windows10. However,while executing through the command prompt by going to the path where this script is located,it throws pop up windows saying "how do you want to open this file?".Command in the script as below:
cd E:/source
for f in *.txt
do
cp -v "$f" E:/Destination/"${f%.txt}"$(date +%m%d%y).txt
done
Please help to fix this error.Thanks in advance.
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
i have a small script that runs a jar file :
#!/bin/bash
prefix="foo";
name=`ls ${prefix}*.jar`;
echo $name;
java -jar $name prop1.properties prop2.properties
when i run it in the terminal using ./myscript.sh, it works fine and the jar file executes, but when i rename it in myscript.command and double click it, i have this error :
ls: foo*.jar : No such file or directory
I saw that apparently a .command file opens a terminal at the root directory, so I tried finding the directory containing myscript.command using that :
dir = 'find <dir> -maxdepth 1 -type d -name '*myDirContainingJar*' -print -quit'
cd $dir
but dir is just blank, any ideas ???
Opening a shell script from Finder on macOS (whether it has extension .command or is an extension-less executable shell script) makes the current user's home directory the current directory (as of macOS 10.12).
To explicitly change to the directory in which the script itself is located in a bash script, use:
cd -- "$(dirname -- "$BASH_SOURCE")"
In this invocation scenario, the following POSIX-compliant variation, which uses $0 in lieu of $BASH_SOURCE, would work too: cd -- "$(dirname -- "$0")"
Note:
Generally, if your script is invoked via a symlink and you want to change to the target's directory (the actual script file's directory as opposed to the directory in which the symlink is located), more work is needed - see this answer of mine.
When opening a script from Finder, however, this issue does not apply, because Finder itself resolves a symlink to its (ultimate) target and then invokes the target directly.
The problem with your script is that it runs with a different working directory than you tested it with. When you do ls something in a script, the script looks in the current working directory (where you cded last in case you're in a terminal), not in the directory the script is in. In general, when writing scripts like this you should use the full path of the file you're referring to or figure out the directory of the script and point to the file relative to that. One solution works in Bash, but it might not in the shell you're using.
I have a script to copy the files from one location to another where i am passing the first location as parameter to the script
#!/bin/bash
locatn=$1
echo $locatn
cp -r /locatn/ /ws/priyapan-rcd/workspace/automation/
but when i run this code this throws error as
cp: cannot stat `locatn': No such file or directory
what could be the issue
Formatting looks a bit weird but as #Patick Trentin said you simply forgot a $ making your script always copy the files to the same location ignoring the given parameter.
#!/bin/bash
locatn=$1
echo $locate
cp -r /${locatn}/ /ws/priyapan-rcd/workspace/automation/
I am trying to send a file or folder to Desktop with Linux Command Prompt but I don't know how.
Please tell me what command can I use for this?
The move command mv. Use man mv for more information, as this command is a lot more complex than it seems. With cd Desktop/ you should be able to find your desktop on variations of linux like Mint or Ubuntu. To find your present working directory, as in your current path for the terminal, type pwd. This will give you your directory which will be similar to /home/Desktop.
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.