shell scripting, how to run different code based on selection? - bash

I have a shell script as below...
#!/bin/ksh
set_logging() {
if [ -f "$LOG_FILE" ]
then
mv $LOG_FILE $LOG_FILE.$SYNCDATE
else
touch $LOG_FILE
fi
# set logging for stdout and stderr (run this if user selects start)
exec >> $LOG_FILE
exec 2>&1
# run this if user selects dry
#exec something else
#exec something else
}
set_logging
# menu
if [[ ! -n $1 ]] ; then
clear
echo ""
echo "What are you trying to do?"
echo "start"
echo "dry"
echo "help"
echo ""
exit 99
fi
case "$1" in
start) run_tar;;
dry) nothing_here_yet;;
help) print_help;;
*) clear
echo "Your syntax is incorrect!"; exit 99;;
esac
This is a snipped of the actual script, running on AIX 7.1 UNIX OS.
What I am trying to do is based on user selection on the menu, change what parts get executed within the set_logging() block (you'll notice I commented the code I want to run if user selects different menu option just to help this example).
Again, this is an example to keep it simple so I understand and can use it in different parts of my shell script.
Anyone able to help?

I'm not totally clear of some details. If you put the following code at the top where the comments are, then it won't really work because $1 might not be set. It needs to go after the test if $1 is empty. For what you describe, you don't need something as complex as a case statement. You can do it with an if statement like:
if [[ "$1" = "dry" ]] ; then
exec something
exec somethingelse
fi
You want the $1 inside quotes. Otherwise, if it is not set, $1 disappears entirely and the statement becomes invalid. Likewise with the if test that you have, you should put the $1 inside double quotes.
You can use = or == depending upon lots of factors. Generally = and == are equivalent with a few exceptions (e.g. very old bourne shell does not have ==, etc.)
Last [[ ! -n $1 ]] is probably better written [[ -z "$1" ]]

Related

Why does $# always return 0?

I'm trying to write a script that will only accept exactly one argument. I'm still learning so I don't understand what's wrong with my code. I don't understand why, even though I change the number of inputs the code just exits. (Note: I'm going to use $dir for later if then statements but I haven't included it.)
#!/bin/bash
echo -n "Specify the name of the directory"
read dir
if [ $# -ne 1 ]; then
echo "Script requires one and only one argument"
exit
fi
You can use https://www.shellcheck.net/ to double check your syntax.
$# tells you how many arguments the script was called with.
Here you have two options.
Option 1: Use arguments
#!/bin/bash
if [[ $# -ne 1 ]]
then
echo "Script requires one and only one argument"
exit 1
else
echo "ok, arg1 is $1"
fi
To call the script do: ./script.bash argument
Use [[ ]] for testing conditions (http://mywiki.wooledge.org/BashFAQ/031)
exit 1: by default when a script exists with a 0 status code, it means it worked ok. Here since it is an error, specify a non-zero value.
Option 2: Do not use arguments, ask the user for a value.
Note: this version does not use arguments at all.
#!/bin/bash
read -r -p "Specify the name of the directory: " dir
if [[ ! -d "$dir" ]]
then
echo "Error, directory $dir does not exist."
exit 1
else
echo "ok, directory $dir exists."
fi
To call the script do: ./script.bash without any arguments.
You should research bash tutorials to learn how to use arguments.

bash programming - best practice setting up variables within a loop or not

I have the following logic in my bash/shell script. Where essentially, I'm trying to pass one argument manually and then passing in other values from a hidden file, like so:
if [[ $# != 1 ]]; then
echo "./tstscript.sh <IDNUM>" 2>&1
exit 1
fi
MYKEY=/dev/scripts/.mykey
if [ -f "$MYKEY" ]
then
IFS=';'
declare -a arr=($(< $MYKEY))
# DECLARE VARIABLES
HOSTNM=localhost
PORT=5432
PSQL_HOME=/bin
IDNUM=$1
DBU1=${arr[0]}
export HOSTNM PORT PSQL_HOME IDNUM DBU1 DBU2
$PSQL_HOME/psql -h $HOSTNM -p $PORT -U $DBU1 -v v1=$IDNUM -f t1.sql postgres
else
echo "Mykey not found"
fi
rt_code=?
exit 1
Am I declaring my variables in the right place? Should it be declaring within my if statement?
Most of your variables are redundant. psql already has a few well-known environment variables it will use if you don't specify various parameters on the command line. The others are just hard-coded, so it's not really important to define them. It really doesn't matter much where you define them, as long as you define them before they are used, since this isn't a very large script. It's a good sign that you've outgrown shell script and are ready for a more robust programming language when you start worrying about the design of the shell script.
if [[ $# != 1 ]]; then
echo "./tstscript.sh <IDNUM>" 2>&1
exit 1
fi
MYKEY=/dev/scripts/.mykey
if ! [ -f "$MYKEY" ]; then
echo "Mykey not found"
exit 1
fi
# You only use the first word/line of the file,
# so this should be sufficient.
IFS=";" read -a arr < "$MYKEY"
export PGHOST=localhost
export PGPORT=5432
export PGUSER=${arr[0]}
: ${PSQL_HOME:=/bin}
"$PSQL_HOME"/psql -v v1="$1" -f t1.sql postgres
When you fill /dev/scripts/.mykey with lines in the form key=value, you can source that file.
$ cat /dev/scripts/.mykey
DBU1=noober
FIELD2="String with space"
echo "Keep it clean, do not use commands like this echo in the file"
In your script you can activate the settings by sourcing the file
if [ -f "${MYKEY}" ]; then
. "${MYKEY}"
# Continue without an array, DBU1 and FIELD2 are set.

Monitor folders using unix bash

I need a shell script that will monitor all the folders given in the command
and will notify the user if a certain file will be created inside them (the name of
the file will be read from keyboard).
I am allowed to use simple commands, so not inotify...
this is what i managed to do so far:
#!/bin/bash
echo "Please enter a file you want to monitor: "
read file_monitor
#this is an infinite while
while [ 1 ] ; do
#using test -e to search for the file
test -e $file_monitor && echo "The file has been created!"
sleep 5
done
I have to find a way to stop the while when the file has been created, and also to search for the file in the folders given in the command line. Can someone help me, please?
To exit the loop, use break:
test -e $file_monitor && echo "The file has been created!" && break
I would prefer to break first, and echo after the loop, or as #mkemp6 suggested, directly use the test as the condition for the loop.
To check the folders, simply loop through them, and check the file in each one.
break [n]
Exit from within a for, while, until, or select loop. If n is specified, break n
levels. n must be ≥ 1. If n is greater than the number of enclosing loops, all
enclosing loops are exited.
while ! test -e "$file_monitor"; do sleep 5; done
But you are much better off using something like inotify to monitor the appropriate directories.
#!/bin/bash
arr=$#
for i in $arr; do
if [ ! -d $i ]
then echo "The parameter $i is no a directory!"
exit 1
fi
done
echo -n "Please give file you want to monitor: "
read file_monitor
a=1
while [ $a -eq 1 ]; do
for i in $arr; do
cd $i
test -e $file_monitor && echo "The file has been created" && a=$((a+1))
cd ..
done
done
So this is what I have managed to do.

Bash + check for file exist with a path home ~

I haven't found anything to deal with this particular situation. Maybe there is a easy way that I'm overlooking instead of checking for a string to catch this scenario. When I check an input for existence of a file, if the input is ~/filecheck , this won't work. I get negative results while the file is in my home folder. Any suggestions for improvement to any part of the script I will definitely appreciate. I also have to use an input instead of a argument. Thanks for any help.
my test script
read -p "Enter: " input
echo $input
if [ -f $input ]; then
read -p "Do you REALLY want to delete this file?:" input2
if [[ $input2='y' || $input2 = 'Y' ]]
then
rm -f $input
elif [[ $input2='n' || $input2='N' ]]
then
exit
else
echo "Invaild Option"
exit
fi
else
echo Invaild Option!
exit
fi
Since you are entering input string as ~/filecheck shell doesn't expand tilde while using condition with -f in [ -f $input ]
You can use it this way but it is not recommended and potentially dangerous as arbitrary commands can be run by user:
if [[ -f $(bash -c "echo $input") ]]; then
echo "file exists"
fi
EDIT: As per the comments below to avoid risky bash -c you can use:
if [[ -f "${input/\~/$HOME}" ]]; then
echo "file exists"
fi
You can't have tilde expansion in this part of the program without using something based on eval—and you don't want to do that with user input. So, your poor-man solution will be to substitute any potential leading ~/ with the expansion of $HOME/. Here's the adaptation of your script in an arguably better style:
#!/bin/bash
read -e -p "Enter: " input
input=${input/#~\//$HOME/} # <--- this is the main idea of this answer (and it's rather poor)
echo "$input"
if [[ -f $input ]]; then
read -e -p "Do you REALLY want to delete this file? " input2
if [[ ${input2,,} = y ]]; then
rm -f -- "$input"
elif [[ ${input2,,} = n ]]; then
exit
else
echo "Invalid Option"
exit
fi
else
echo "Invalid Option!"
fi
exit
Now, out of curiosity, why are you spending time to make a wrapper around rm? you're making a clunky interface to an already existing program, without adding anything to it, only rendering it less powerful and less easy to use.
If all what you want it's to ask the user before deleting, you can use:
rm -i
This will give you appropriate error in the case file does not exist.

Unix while loop to test command line argument if it is a directory

Trying to make a script that will take a command line argument as a pathname and then test if it is a working directory. Then I wish to run commands (tests) on the directory such as how many files in what sub directories etc.
I am unable to think of a logic to test this with. How would you determine if the argument is a directory?
This is what I have tried
if [ -d "$1" ]
then
echo "It works"
fi
I dont get "It works" when I try this. So I switched it to -a for a file because it is easier to test. And again, I do not get the echo output.
Use the -d option to the test command.
if [ -d "$1" ]
then ...
fi
The title mentions a while loop, but none of the previous commentary seems to mention that. You might try a simple script like:
#!/bin/sh
for arg; do
if test -d "$arg";
echo "$arg is a directory"
else
echo "$arg is not a directory"
fi
done
For variety, you could rewrite that as:
#!/bin/sh
for arg; do
not=$(test -d "$arg" || echo "NOT ")
echo "$arg is ${not}a directory"
done

Resources