I have a C program and a folder that contains files and I need to create a shell script that execute that C program and receives a file (one by one) name as argument which is read by the command line. How can I do this?
You could try the following shell code :
#!/bin/sh
cd /PATH/TO/DIR
for i in *; do./C_APP "$i"; done
Related
Let's say I have a bash script called program stored in /home/user/program. But then I create a sym link to the bash script using ln -s /home/user/program /usr/bin/program. Then I cd /usr/home/anotherdirectory and then run program. How do I get the bash script to print /usr/home/anotherdirectory to tell me where it was called from?
I tried echo $PWD and it only printed out /usr/bin
This should do the job in your script:
dirname $(readlink -e "$0")
From man readlink:
-e:canonicalize by following every symlink in every component of the given name recursively, all components must exist
This could be a dumb question, but how do I make it so a script can be called with "sh [Script Name] [File Path]" and have the script automatically read the file path?
For instance, if I use cp ~/usr/local/bin/testScript ~/Desktop it knows that the target file is the testScript file because it read the path from the command line.
I have a script based on ImageMagick that prompts the user for the file path using "read", processes it, and then drops it onto the desktop. I want the script to skip the actual prompt for the file path, and instead just target whatever file path is entered after calling the script (I.E. sh ConvertPDF /Users/ProfileName/Desktop/testFile.tiff
Positional parameters to the script are available via $n where n >= 1. For n > 9 braces are required in order to distinguish from n < 10.
#!/bin/sh
echo "$2" "$3" "$1"
...
$ ./somescript foo bar 42
Hello I am trying to write a Bash Script that will loop through a directory, and run the files in that directory through a command line program.
Unfortunately when I run it I keep getting
/home/user/Documents/Original_Files/*.fastq.gz: No such file or directory
Here's my code
Origin=/home/user/Documents/Original_Files/*.fastq.gz
for a in "$Origin"
do
BASE=basename "$a"
nohup java -jar $
done
Use an array if you want to keep several values in a variable.
Origin=(/home/user/Documents/Original_Files/*.fastq.gz)
for a in "${Origin[#]}" ; do
BASE=$(basename "$a")
nohup java -jar "$BASE"...
I write up a little shell script in bash that allows me to execute commands in sub-directories. Here is the script
bat.sh:
#!/bin/sh
for d in */; do
echo "Executing \"$#\" in $d"
cd $d
`$#`
cd ..
done
With my following directory structures
/home/user
--a/
----x.txt
----y.txt
--b/
----u.txt
----v.txt
I expect the following command to list out the content of directories a and b when it is executed in the home directory
bat.sh ls
The result is
Executing "ls" in a/
/home/user/bin/bat.sh: line 6: x.txt: command not found
Executing "ls" in b/
/home/user/bin/bat.sh: line 6: u.txt: command not found
Any idea on what is going wrong here?
You don't want the back quotes; you want double quotes.
#!/bin/sh
for d in */
do
echo "Executing \"$*\" in $d"
(cd "$d" && "$#")
done
You are trying to execute the output of the command you pass, whereas you simply want to execute the command.
The use of an explicit subshell (the ( … ) notation) may avoid some problems with symlinks that jump to other directories. It is, in my (perhaps archaic) view, a safer way to switch directories for the purposes of executing commands.
We now to find the directory of a shell script using dirname and $0, but this doesn't work when the script is inluded in another script.
Suppose two files first.sh and second.sh:
/tmp/first.sh :
#!/bin/sh
. "/tmp/test/second.sh"
/tmp/test/second.sh :
#!/bin/sh
echo $0
by running first.sh the second script also prints first.sh. How the code in second.sh can find the directory of itself? (Searching for a solution that works on bash/csh/zsh)
There are no solution that will work equally good in all flavours of shells.
In bash you can use BASH_SOURCE:
$(dirname "$BASH_SOURCE")
Example:
$ cat /tmp/1.sh
. /tmp/sub/2.sh
$ cat /tmp/sub/2.sh
echo $BASH_SOURCE
$ bash /tmp/1.sh
/tmp/sub/2.sh
As you can see, the script prints the name of 2.sh,
although you start /tmp/1.sh, that includes 2.sh with the source command.
I must note, that this solution will work only in bash. In Bourne-shell (/bin/sh) it is impossible.
In csh/tcsh/zsh you can use $_ instead of BASH_SOURCE.