Location=$1? what does it mean? - unix

#!/bin/bash
LOCATION=$1
FILECOUNT=0
DIRCOUNT=0
if [ "$#" -lt "1" ]
then
echo "Usage: ./test2.sh <directory>"
exit 0
fi
I don't actually get what the If statement is saying can anyone help me to explain this?Thank you

$1 refers to the first argument of the bash file. In this case, you can pass your directory path by issuing the following command:
# ./test2.sh /path/of/your/directory
#!/bin/bash
LOCATION=$1 #first argument of the script
FILECOUNT=0
DIRCOUNT=0
if [ "$#" -lt "1" ] #if the number of argument(s) ($#) is less than 1
then
echo "Usage: ./test2.sh <directory>"
exit 0
fi
You can read this article for more information about parameter passing.
Hope it helps.

$1 is the first argument that is passed to the bash script. If you start the script like ./test2.sh argument1 argument2 the $1 will refer argument1.
The if-statement checks, if the count of arguments (that's the $#) is smaller than 1, then it will output the usage statement (as it seems you can't run the script without any argument).

Related

Unix partial string comparison

I need to compare a string partially to check for a given condition.
Like my $1 will be checked if it has a part of a string BLR
while my file input has $1 entries as BLR21 BLR64 IND23
I only need a true condition when $1 is equal to BLR**
where these stars can be anything.
I used a simple if condition
if($1=="BLR21")
{print $2}
Now this only works when whole BLR21 is there in row.
I need to ckeck not for BLR21 but only BLR.
Please Help
Your question is not great, I hope I understood.
Quick and easy solution
grep BLR input.txt
This will output all the lines in which "BLR" is found, in file input.txt. It will match "BLR" with any prefix and suffix, whatever they might be (spaces, alphanumerical, tabs, ...).
"Complicated" solution
A bit more complicated. It does the same thing, but makes sure input.txt exists, and is in the form of a script.
Input file, input.txt:
BLR21 BLR64 IND23
Your script could be:
#!/bin/bash
#
# Arguments
inputfile="input.txt"
if [[ $# -ne 1 ]]
then
echo "Usage: myscript.bash <STRING>"
exit 1
else
string="$1"
fi
# Validation, and processing...
if [[ ! -f "$inputfile" ]]
then
echo "ERROR: file >>$inputfile<< does not exist."
exit 2
else
grep "$string" "$inputfile"
fi
And to call the script, you do:
./myscript.bash BLR
But really, a simple grep does the job here.
Taking it even further...
#!/bin/bash
#
# Arguments
inputfile="input.txt"
if [[ $# -ne 1 ]]
then
echo "Usage: check.bash <STRING>"
exit 1
else
string="$1"
fi
# Validation, and processing...
if [[ ! -f "$inputfile" ]]
then
echo "ERROR: file >>$inputfile<< does not exist."
exit 2
else
while read -r line
do
if [[ "$line" =~ $string ]]
then
echo "$line"
fi
done <"$inputfile"
fi
Now this one is like going to the moon via mars...
It reads each line of the file, one by one. Then it checks if that line contains the string, using the =~ operator inside the if.
But this is crazy, when a simple grep would do.

How to catch "$variable is not defined" in jq?

Let's pretend I'm running something like this:
jq -nr --arg target /tmp \
'(["echo","Hello, world"]|#sh)+">\($target)/sample.txt"' \
| sh
Everything is fine unless I forgot to pass variable $target:
$ jq -nr '(["echo","Hello, world"]|#sh)+">\($target)/sample.txt"'
jq: error: $target is not defined at <top-level>, line 1:
(["echo","Hello, world"]|#sh)+">\($target)/sample.txt"
jq: 1 compile error
How can I catch this and use default value?
I've tried:
$target?
($target)?
try $target catch null
$target? // null
But it seems to be parsing-time error, which obviously can't be caught at runtime. Have I've missed any dynamic syntax?
I've found that command-line arguments can be found in $ARGS.name, but there are two drawbacks:
This was introduced in version 1.6, but I have 1.5 on CentOS 7.
It doesn't catch locally defined variables.
Assuming you need to do something more useful with jq than write 'Hello World' over a text file. I propose the following,
Maybe we can learn some programming tips from Jesus:
"Give to Caesar what belongs to Caesar, and give to God what belongs to God"
Suppose that Caesar is bash shell and God is jq, bash is appropriate to work and test the existence of files, directories and environment variables, jq is appropriate to process information in json format.
#!/bin/bash
dest_folder=$1
#if param1 is not given, then the default is /tmp:
if [ -z $dest_folder ]; then dest_folder=/tmp ; fi
echo destination folder: $dest_folder
#check if destination folder exists
if [ ! -d $dest_folder ]
then
echo "_err_ folder not found"
exit 1
fi
jq -nr --arg target $dest_folder '(["echo","Hello, world"]|#sh)+">\($target)/sample.txt"' | sh
#if the file is succesfully created, return 0, if not return 1
if [ -e "$dest_folder/sample.txt" ]
then
echo "_suc_ file was created ok"
exit 0
else
echo "_err_ when creating file"
exit 1
fi
Now you can include this script as a step in a more complex batch, because it is congruent with linux style, returning 0 on success.

My exit commands are not working right

I am not exactly sure if i am using the exits correctly. But when i execute the code with something that prints the usage statement it should stop there.
It should do one or the other. In my case it is doing both.
cmd="$1" ## the command to find
if [[ $# -ne 1 ]]
then
echo "usage: ./findcmd command"
fi
exit=1
path=$(echo $PATH | tr ":" " ")
for dir in $path
do
if [[ -x "$dir/$cmd" && -r "$dir/$cmd" ]]
then
echo "$dir/$cmd"
exit 0
fi
done
echo "$cmd not on $PATH"
exit=0
OUTPUT:
[112] ./findcmd
usage: ./findcmd command
/usr/local/bin/ **this should not be here
[113] ./findcmd ping
/usr/bin/ping
You use exit correctly to stop the script after your dir/cmd gets printed; try using it that way elsewhere.
Keep in mind that your first exit, once correct, will stop the script before the loop whether usage was printed or not.
It should be exit [n].
Within a script, an exit nnn command may be used to deliver an nnn
exit status to the shell (nnn must be an integer in the 0 - 255
range).
And it should be inside the IF/FOR block.
Like this:
cmd="$1" ## the command to find
if [[ $# -ne 1 ]]
then
echo "usage: ./findcmd command"
exit 1
fi
## rest of code to execute if args are correct

How To Check For Directory On A Give Argument

If the argument is 0 then script should check directory called “App0” is in the windows path variable. If not exists, then add \App0 in the path. I Am Struggling To Understand ( If the argument is 0 ).
My Work So Far.
if [ -d "${Appo}" ]; then
echo "Appo Doesn't Exist."
mkdir Appo
echo "File Created"
fi
Thank You
#!/bin/sh
if [[ $# == 0 ]]
then
echo "zero args"
fi
for arg in "$#" # You might get more than one argument.
do
dir="App${arg}" # Make the name by combining the strings.
if [[ -d $dir ]]
then
echo "App$arg exists"
else
mkdir "$dir" # Be careful the name supplied may contain spaces.
echo "Created directory: $dir"
fi
done

Unix determine if a file is empty

I am attempting to make a script that will check to see if there is any tyext within a file. I have developed the following script. I have made it check to see if there is exactly 2 arguments, see if the file exists, but I am having trouble checking the file for text within it. The code is as follows:
#!/bin/ksh
#check if number of arguments are 2
if [ $# -ne 2 ]; then
echo "Does not equal two arguments"
echo "Usage $0 inputfile outputfile"
exit 1
fi
#check if input file exists
if [ ! -f $1 ]; then
echo "$1 not found!"
exit 1
fi
#Check if input file is null
#This next block of code is where the issue is
if [ grep -q $1 -eq 0 ]; then
echo "$1 must have text within the file"
exit 1
fi
Any help would be appreciated
test's "-s" option checks if the file is empty -- see manual. So your last chunk would become
#Check if input file is null
#This next block of code is where the issue is
if [ ! -s $1 ]; then
echo "$1 must have text within the file"
exit 1
fi
Try using stat
stat -c %s filename

Resources