I need to create an automation that takes files as inputs and uses their location string as an input variable to a script. I also need it ask for a string before running the script. How would I go about doing this?
The command line code I want to execute:
convert (file-location) -page '(string)' (file-location)
Thanks in advance!
How about this?
#!/bin/bash
if [ $# -ne 2 ]
then
echo "Usage: $0 <path_to_input_file>"
fi
echo "Processing $1. Enter string: "
read a
convert $1 -page "$a" $1
You invoke this script with one file name at a time, for that file it asks you for the string to be used. You could rewrite this to loop over a bunch of files too.
Related
I have written a small script that will take the users input and then generate the md5sum values for it
count = 0
echo "Enter number of records"
read number
while [ $count -le $number ]
do
echo "Enter path"
read path
echo "file name"
read file_name
md5sum $path"/"$filename #it shows the md5sum value and path+filename
((count++))
done
How can I pass these values ( path,file name, and md5sums ) to CSV file. ( assuming the user chooses to enter more than 1 record)
The output should be like
/c/training,sample.txt,34234435345346549862123454651324 #placeholder values
/c/file,text.sh,4534534534534534345345435342342
Interactively prompting for the number of files to process is just obnoxious. Change the script so it accepts the files you want to process as command-line arguments.
#!/bin/sh
md5sum "$#" |
sed 's%^\([0-9a-f]*\) \(\(.*\)/\)?\([^/]*\)$%\3,\4,\1%'
There are no Bash-only constructs here, so I switched the shebang to /bin/sh; obviously, you are still free to use Bash if you like.
There is a reason md5sum prints the checksum before the path name. The reordered output will be ambiguous if you have file names which contain commas (or newlines, for that matter). Using CSV format is actually probably something you should avoid if you can; Unix tools generally work better with simpler formats like tab-delimited (which of course also breaks if you have file names with tabs in them).
Rather than prompting the user for both a path to a directory and the name of a file in that directory, you could prompt for a full path to the file. You can then extract what you need from that path using bash string manipulations.
#!/bin/bash
set -euo pipefail
function calc_md5() {
local path="${1}"
if [[ -f "${path}" ]] ; then
echo "${path%/*}, ${path##*/}, $(md5sum ${path} | awk '{ print $1 }')"
else
echo "
x - Script requires path to file.
Usage: $0 /path/to/file.txt
"
exit 1
fi
}
calc_md5 "$#"
Usage example:
$ ./script.sh /tmp/test/foo.txt
/tmp/test, foo.txt, b05403212c66bdc8ccc597fedf6cd5fe
I would like to know how to access the contents of a variety of txt files by passing arguments into shell scripts. I'll have different files and I'm expecting to execute with this command:
./script.sh FileA.txt
What should I put into my shell script so that I can access and manipulate the contents of the files?
I tried this but it outputs 0:
echo "$#"
I also tried these, but both output nothing:
for i in $1
do
echo "$i"
done
echo "$1"
To sum up the contents see this link to understand bash arguments more https://tecadmin.net/tutorial/bash-scripting/bash-command-arguments/ . Also as #Barmar said, to iterate through a list of arguments of unknown quantity use for i in "$#" .
edit
and as #Barmar said, $1 is simply the name of the argument. So echoing $1 will just echo the name.
I don't understand your question fully. Lets assume you have list of file names in a text file "FileA.txt".
And you wanted to run some commands for each file in the "FileA.txt" file.
Can you try below:
for i in `cat $1`
do
echo $i
done
I am trying to pass a user-input variable (a file name) into a foreach loop in tcsh. The user entered variable is, for example, "files.list" (saved in the same folder as the Shell Script is saved, and is being run from).
Here is my code:
#! /usr/bin/tcsh -f
echo please enter files list
set x = $<
foreach i ('$x')
echo $i
end
What I want is for each of the words in "files.list" to be output to the screen. Files.list contains 5 lines, each with a file name.
myScript22.sh
Mad45.sh
Number32.sh
killBill.sh
gotMilk.sh
bugslife.sh
I get an error - "foreach: Words not parenthesized."
Could it be that 'cat $x' isn't calling the x variable correctly? If so, how do I get the file set up so it's contents can be looped thru?
Any help is appreciated!
If you really really really really have to use tcsh, then the following is the propper way to do it:
#!/usr/bin/tcsh -f
echo please enter files list
set x = $<
foreach line (" `cat $x` ")
echo "$line"
end
It is important to see that the cat command is between <double-quotes>. The difference is that otherwise, the foreach statement will read word-by-word, while the double-quoted version will read line-by-line. The logic of this is ... questionable. Also, I quoted the variable line in the echo statement, because it will actually complain when it hits an empty line.
In bash, you would just do the following nice thing:
#!/usr/bin/env bash
echo "Please enter files list"
file=""
while [[ ! -e $file ]]; do read -r file; done
while read -r line; do
echo "$line"
done < "$file"
Very important reads:
CSH PROGRAMMING CONSIDERED HARMFUL
Top Ten Reasons not to use the C shell
For reference purposes, I was missing the cat command, before the $x.
So the code should read:
#! /usr/bin/tcsh -f
echo please enter files list
set x = $<
foreach i ('cat $x')
echo $i
end
You have two mistakes in your tcsh script:
Missing cat command in front of the filename in your foreach condition.
Use of straight single quotes(' ') instead of backquotes (``) in your foreach condition.
The following script should work for you.
#!/usr/bin/tcsh -f
echo please enter files list
set x = $<
foreach i(`cat $x`)
echo $i
end
Given file socat.conf
AUTOSTART=default
SOCAT_default="TCP4-LISTEN:3724,nodelay,fork,reuseaddr,su=nobody TCP4:your.wow.server.ip.address:3724,nodelay"
The relevant part of the bash script that reads this file:
[ ! -f /etc/default/socat.conf ] || . /etc/default/socat.conf
start () {
echo "Starting $DESC:"
maxfds
umask 027
cd /tmp
if test "x$AUTOSTART" = "xnone" -o -z "x$AUTOSTART" ; then
echo "Autostart disabled."
exit 0
fi
for NAME in $AUTOSTART ; do
ARGS=`eval echo \\\$SOCAT_$NAME`
echo $ARGS
start_socat
echo " $NAME $ARGS"
done
return $?
}
For the full file see here: https://blog.bentrax.de/2009/08/26/socat-start-automatisieren-und-iptables-regeln-laden/
My question is, how can I add another command to socat.conf? I tried with
AUTOSTART=default,another
SOCAT_default="TCP4-LISTEN:3724,nodelay,fork,reuseaddr,su=nobody TCP4:your.wow.server.ip.address:3724,nodelay"
SOCAT_another="..."
However this did not work. I am not very familiar with bash scripts to understand the for NAME in $AUTOSTART loop. I think the answer lays there. Any ideas?
The for NAME in $AUTOSTART works by splitting $AUTOSTART into words using the environmental variable $IFS as delimiters (default is space, tab and newline). Each word in turn is then stored in $NAME and processed within the loop until no words remain.
The solution to your problem, then, is to separate your words using spaces (or tabs, or newlines..):
AUTOSTART="default another"
The double quotes are necessary, otherwise it will be read as two separate commands, AUTOSTART=default and another (again because of word-splitting using IFS).
Cheers everyone :)
I'm trying to make a linux script. This script shall be called with one parameter, a file stored in my home directory. I can't seem to use
cat $var1 >> $1
So i have this variable $var1 and I want to save it in a file that does exist and its name is given in $1
Anyone help me please!
The cat command shows the contents of a file.
Unless the value of $var1 is a file that you want to 'copy' to $1, it won't work (probably gives a 'file not found' kind of error).
The easiest solution, I can think of, is to echo the variable:
echo "$var1" >> "$1"
As stated by #glglgl it is better to put the variable between double quotes. They prevent spaces messing up the command as they split a parameter into multiple parameters
Then you want to create a script that saves things in the file you indicate as a parameter.
file_name=$1 #you get the parameter
...do things...
echo "everything you've done" >> $file_name #case want to append
echo "everything you've done" > $file_name #case want to overwrite