I am trying to run script on output files that can be further used as input files for gaussian.
I wanted to know what are the commands used in Linux to run the script on .log files and .HSCP1 files.
Many thanks,
Regards,
The generic syntax of passing an argument to a script in linux, assuming your script is named script.sh and your target file is named arg.log, would be
script.sh arg.log
This assigns the name arg.log to $1 inside the environment of the executing copy of script.sh. If you don't then do something with that, it won't matter.
You might also have your script read its stdin like this:
script.sh < arg.log
which will put the contents os arg.log on script.sh's stdin, but unless it reads them accordingly, it won't matter.
Of course, both these assume script.sh is in your $PATH; otherwise you will need to apply a path for the OS, such as /path/to/dir/with/script.sh or (if you are in the same directory) ./script.sh.
If what you are asking is how to get a lorge number of files assigned as arguments, you could pass wildcards - for the first example above, that could be done as
./script.sh /path/to/*.log /also/to/other.*
or you could use find, maybe with xargs like so -
find /path/to/files/ -name *.log | xargs /path/of/script.sh
which will call the script over and over.
I hope one of these helps, but you really must provide more context for what you are doing and how.
Related
I'm trying to store the result of this command that is written in a script
ls -l /etc|wc -l
in a variable on another file.
To summarize, I have a script with that command and when I execute it, I want the result to be stored in a variable in another file.
Can someone help me with this please?
You may try to use temporary file (if possible).
This command:
ls -l /etc|wc -l > /tmp/myvar.txt
Another file:
myvar="$(cat /tmp/myvar.txt)"
You just need to use '> path/to/file' at the end of your command to redirect the output to a file (this will override the file content).
If you need another behavior, like append the content, you should use '>>' instead of '>'.
Take a look here for more details.
I'm not sure I understand what you're trying to do so I'll give you two solutions.
If the command you mention is in some file script_A.sh and you want the results of that script stored in some variable $var when running some other script script_B.sh, randomir's solution is good. In script_B:
var=$(bash path/to/script_A.sh)
If what you're asking is to run script_A.sh and then have it write a new line to a file that would store the results to a value when you run script_B.sh, I suppose you could run something like:
result=$(ls -l /etc|wc -l)
echo "var=\"$result\"" > path/to/script_B.sh
or even replace a line in a script_B.sh that already exists:
result= $(ls -l /etc|wc -l)
sed -i "s|var=SOMEPLACEHOLDER|var='$result'|" path/to/script_B.sh
If the latter is what you want, though, can you tell us more about what you're trying to accomplish? There's probably a better way than what you propose.
I am writing a shell script to sync to a github repo, kick off the build, then take the output file, rename it, and move it to a location where it can be seen by Apache.
It's the renaming of the file that I've got not the faintest how to do within a shell script (I have virtually no experience with shell scripts - my understanding
Compiler will create /var/espbuild/firstpart_1vXX_secondpart.bin
I need to move this file to:
/var/www/html/builds/espbuild/firstpart_1vXX_DATE_secondpart_postfix.bin
1vXX is the version number
DATE is the output of date +%m-%d
postfix is just a string.
I'm not really certain where to start for something like this - I'm sure there's a graceful way, since this is the kind of thing shell scripts are made for, but I know just about nothing about shell scripts.
Thanks in advance
You can get the result of a command into a variable by using $():
DATE=$(date +%m-%d)
Then just use it in the new filename:
INPUT=/var/espbuild/firstpart_1vXX_secondpart.bin
OUTPUT=/var/www/html/builds/espbuild/firstpart_1vXX_${DATE}_secondpart_postfix.bin
mv ${INPUT} ${OUTPUT}
Edit: To get out the version part, here's a quick example:
VERSION=$(grep -o 1v.. <<< ${INPUT})
Then OUTPUT should be set like:
OUTPUT=/var/www/html/builds/espbuild/firstpart_${VERSION}_${DATE}_secondpart_postfix.bin
You can use this in BASH:
f='/var/espbuild/firstpart_1vXX_secondpart.bin'
s="${f##*/}"
s2=${s##*_}
dest="/var/www/html/builds/espbuild/${s%_*}_$(date '+%m-%d')_${s2%.*}_postfix.bin"
echo "$dest"
/var/www/html/builds/espbuild/firstpart_1vXX_07-14_secondpart_postfix.bin
cp "$f" "$dest"
I am wondering that how can I Use the file command and determine if a file is a script or not.for example in usr bin I want to know which file is script or not. actually i don't want write any script just i need a command for determine that.
You can certainly trust file to find any script in the directory you specify:
file /usr/bin/* | grep script
Or, if you prefer to do it yourself and you are using bash you can do:
for f in /usr/bin/*; do r=$(head -1 $f | grep '^#! */') && echo "$f: $r"; done
which uses the shebang to determine the interpreter and thus the script entity.
This should work (assuming that you're using BASH):
for f in `ls`; do file $f|grep "executable"; done
Update- I just validated that this works for C shell scripts, BASH, Perl, and Ruby. It also ignores file permissions (meaning that even if a file doesn't have the executable bit set, it still works). This seems to be do to the file command looking for a command interpreter (bash, perl, etc…)
file can't guarantee to tell you anything about a text file, if it doesn't know how to interpret it.
You may need to do a combination of things. jschorr's answer should probably work for the stuff in /bin, but another way to test a file might be to check whether a text file is executable.
stat -c "%A" myfilename | grep x
If that returns anything, then your file has execute permissions on it. So if file gets you a description that tells you it's plain text (like "ASCII text"), and there are execute permissions on the file, then it's a pretty good bet that it's a script file.
Not perfect, but I don't think anything will be.
Can I execute command within another command in UNIX shells?
If impossible, can I use the output of the previous command as the input of next command, as in:
command x then command y,
where in command y I want use the output of command x?
You can use the backquotes for this.
For example this will cat the file.txt
cat `echo file.txt`
And this will print the date
echo the date is `date`
The code between back-quotes will be executed and be replaced by its result.
You can do something like;
x=$(grep $(dirname "$path") file)
here dirname "$path" will run first and its result will be substituted and then grep will run, searching for the result of dirname in the file
What exactly are you trying to do? It's not clear from the commands you are executing. Perhaps if you describe what you're looking for we can point you in the right direction. If you want to execute a command over a range of file (or directory) names returned by the "find" command, Colin is correct, you need to look at the "-exec" option of "find". If you're looking to execute a command over a bunch of arguments listed in a file or coming from stdin, you need to check out the "xargs" commands. If you want to put the output of a single command on to the command line of another command, then using "$(command)" (or 'command' [replace the ' with a backquote]) will do the job. There's a lot of ways to do this, but without knowing what it is you're trying it's hard to be more helpful.
Here is an example where I have used nested system commands.
I had run "ls -ltr" on top of find command. And it executes
it serially on the find output.
ls -ltr $(find . -name "srvm.jar")
I want to write a very simple script , which takes a process name , and return the tail of the last file name which contains the process name.
I wrote something like that :
#!/bin/sh
tail $(ls -t *"$1"*| head -1) -f
My question:
Do I need the first line?
Why isn't ls -t *"$1"*| head -1 | tail -f working?
Is there a better way to do it?
1: The first line is a so called she-bang, read the description here:
In computing, a shebang (also called a
hashbang, hashpling, pound bang, or
crunchbang) refers to the characters
"#!" when they are the first two
characters in an interpreter directive
as the first line of a text file. In a
Unix-like operating system, the
program loader takes the presence of
these two characters as an indication
that the file is a script, and tries
to execute that script using the
interpreter specified by the rest of
the first line in the file
2: tail can't take the filename from the stdin: It can either take the text on the stdin or a file as parameter. See the man page for this.
3: No better solution comes to my mind: Pay attention to filenames containing spaces: This does not work with your current solution, you need to add quotes around the $() block.
$1 contains the first argument, the process name is actually in $0. This however can contain the path, so you should use:
#!/bin/sh
tail $(ls -rt *"`basename $0`"*| head -1) -f
You also have to use ls -rt to get the oldest file first.
You can omit the shebang if you run the script from a shell, in that case the contents will be executed by your current shell instance. In many cases this will cause no problems, but it is still a bad practice.
Following on from #theomega's answer and #Idan's question in the comments, the she-bang is needed, among other things, because some UNIX / Linux systems have more than one command shell.
Each command shell has a different syntax, so the she-bang provides a way to specify which shell should be used to execute the script, even if you don't specify it in your run command by typing (for example)
./myscript.sh
instead of
/bin/sh ./myscript.sh
Note that the she-bang can also be used in scripts written in non-shell languages such as Perl; in the case you'd put
#!/usr/bin/perl
at the top of your script.