How to write a "bash script.sh argument " [closed] - linux

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
hi can someone help me with this.
How to Write a script that takes in as argument a filename and displays its modification date and time in this way exactly :
[user#localhost...]$ bash script.sh temp.txt
the file temp.txt was modified on May 1 20:20
And then modify that script in such a way that it lists the modification dates for directories whose names contain a given pattern in this way exactly :
[user#local....]$ bash script.sh testRegex Pub
the file testRegex was modified on May 1 20:22
the directory /home/user/Public was modified on Dec 26 08:00
the directory /home/user/Pubs. was modified on May 2 20:00
please help I need to answer this fast
Thanks

This is pretty simple to do actually. You should read up on the stat command as #John Bollinger said. I also used the date command to format the date. You can read up on taking arguments for a script here
Combining all of this would give -
#!/bin/bash
filename=$1;
dirname=$2;
file_mod_date=`date -d #$( stat -c %Y $1 ) +%m" "%B" "%H:%M`;
echo "The file ${filename} was modified on ${file_mod_date}";
if [ "$2" == "" ]; then
exit 1;
else
for i in /home/user/*${dirname}*/; do
dir_mod_date=`date -d #$( stat -c %Y $i ) +%m" "%B" "%H:%M`;
echo "The directory ${i} was modified on ${dir_mod_date}";
done
fi

A good way to do this is with passing options and values:
For example:
file_name=""
help_message="To use this script type script.sh --file /path/to/file.txt"
# -- Get input options (if any)
while [[ $# > 0 ]] ;do
key="$1"
case ${key,,} in
-f|--file)
file_name="${2,,}"
shift
;;
-h|--help)
echo -e "$help_message"
exit;
shift
;;
esac
shift
done
Call the script like this:
bash script.sh -f "temp.txt"
With regard to the "logic" of the script, you will have to figure that out ;-)

Related

How to get variables config from shell [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed last month.
Improve this question
cat a.sh
#!/bin/bash
t=1232
echo $1
#echo $t
when i run the script "./a.sh $t" I can't get value 1232
when the script replacement with echo $t ,run the script "./a.sh" can get vaule 1232
can anybody else tell me ,if i use "./a.sh $t" this form ,how can get the vaule,thanks alot
have no ideas to get the variables throug the termi
When you run "./a.sh $t your current shell evaluates $t to '' so $1 is unset in your script and it will just execute echo.
If you quote the the variable either ./a.sh \$t or ./a.sh '$t' your script will do echo '$t'. You can then use either eval to get it to evaluate the expression:
eval echo "$1"
or preferable strip off the leading '$' and use indirect variable:
var=${1:1}
echo "${!var}"
If you just need to store data use json or sqlite instead of a script.
If you have logic in your script consider just passing in the variable names and if not set dump all variables (using the name convention that your variables are lower case):
#!/bin/bash
if [ -z "$1" ]
then
set | grep "^[a-z]"
exit 0
fi
for v in "$#"
do
set | grep "^$v="
done
and you then do:
$ ./a t
t=1232
$ ./a
t=1232
I think your variable t are out of scope. Therefore, what you want to do is assign the variable t=1232, beforehand, and use it as an argument. So the script would be
#!/bin/bash
echo $1
Then call the script as you wanted to with variable t already assigned to the value, so it would print the desired output
t=1232
./script.sh $t
I think that's that. would love to hear your thoughts tho

Converting content of files to uppercase [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
So I have a shell script called "concat" that currently takes command line arguments and prints the contents of files named on the command line. I need to now create a script called "concatconvert" that calls the "concat" script, takes the contents of files and converts them.
The following is the code of my script "concat":
#!/bin/bash
if [ $# -eq 0 ]; then
printf "Usage: concat FILE ... \nDescription: Concatenates FILE(s)
to standard output separating them with divider -----.\n" >&2
exit 1
fi
for var in "$#"
do
if [[ ! -e "$var" ]]; then
printf "One or more files does not exist\n" >$2
exit 1
fi
done
for var in "$#"
do
if [ -f "$var" ]; then
cat $var
printf -- "-----\n"
fi
done
exit 0
I am going to be calling "concat" using
#!/bin/bash
./concat
in the concatconvert script.
Concatconvert is going to take arguments "-u" and "-l"
Ultimately the script would be executed as:
./concatconvert -u test1.txt test2.txt
-u converts contents of files to uppercase.
For example, "This is a test" becomes "THIS IS A TEST".
-l converts contents of files to lowercase.
For example, "This is a test" becomes "this is a test".
Only one option can be provided at a time.
I am not too sure where to begin on this. I appreciate any help.
You should use tr command as mentioned by #jenesaisquoi.
The tr command in UNIX is a command-line utility for translating or
deleting characters.
To use it to change everything to lower case command would be :
echo "This is Test" | tr [:upper:] [:lower:]
this is test
To use it to change everything to upper case command would be :
echo "This is Test" | tr [:lower:] [:upper:]
THIS IS TEST
To use it for a file use below command :
tr '[:upper:]' '[:lower:]' < filename

I would like to understand this shell script [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
This is an assignment I received.
I have been requested to explain what the purpose of this code is. However, when I execute this, there does not appear any output.
As I am new to shell scripting, could somebody help me out?
Thanks a lot.
#!/bin/bash
# $1 = node IP
# $2 = node port
# $3 = hostname to resolve
[[ $# != 3 ]] && logger -p local0.error -t ${0##*/} -- "usage: ${0##*/} <node IP> <node port> <hostname to resolve>" && exit 1
node_ip=$(echo $1 | sed 's/::ffff://')
dig +short #$node_ip $3 IN A &> /dev/null
[[ $? == 0 ]] && echo “UP”
$#: number of parameters passed to your script, if you execute this script like this: bash filename.sh p1 p2 p3 p4, the $# in the filename.sh will be evaluated to 4
$?: return value of the previous command. In shell, return value of non-zero means something wrong happened.
[[ $# != 3 ]] && logger ... && 1: means if the amount of parameters is not 3, then log something and exit with return value 1
node_ip=$(echo $1 | sed 's/::ffff://'): replace the ::ffff: in the first parameter and assign it to node_ip
dig +short #$node_ip $3 IN A &> /dev/null: call dig command and redirect the output to /dev/null, so you can't see any thing printed out. By the way, I don't know what dig does
[[ $? == 0 ]] && echo “UP”: if dig command (namely the previous command) returns a zero value, which means SUCCESS, then print the word UP

How to compare variables in for loop bash [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I want to create if statement, but in this statement are some errors
for i in ${#:2} ; do
if (( $2 -eq $i ))
then
continue
fi
done
How to fix my if statement
Your statement only works for integers.
If you want to compare them as strings, you can use [[ "string1" = "string2" ]]:
$ cat -v myscript
#!/bin/bash
for i in "${#:2}" ; do
if [[ "$2" = "$i" ]]
then
echo "$2 and $i are the same"
else
echo "$2 and $i are different"
fi
done
$ chmod +x myscript
$ ./myscript dummy target foo bar target
target and target are the same
target and foo are different
target and bar are different
target and target are the same
As you can see from this runnable example, it works. If you find that it doesn't on your system, you should provide a complete example like the above demonstrating it.
I would suggest
if [ "$2" = "$i" ]

Show file contents after searching word Done [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I need to display the file contents after searching for a word. If the word is found, display the file.
My code is below:
GNU nano 2.2.6 File: work
#!/bin/bash
while read -p "Welcome what would you like to do (S) to search or (Q) to quit " option
do
case $option in
"S") echo "What is the name of the file you would like to search for?"
read file
echo "What word would you like to find in the file?"
read word
grep -q $word $file
if [ $? -eq 0 ]; then
echo "$word found in $file"
cat $file
else
echo "$word NOT found in $file"
fi
;;
"Q") echo "Goodbye!"
exit ;;
*) echo "invalid option" ;;
esac
done
Replace
echo $file
with
cat $file
I believe you are looking for command cat $file. Stick it inside of your if block.
I need to load up what a file says with out loading up the file.
There is no way to access the contents of the file without accessing the file.
grep -l word file | xargs -r cat
shows file content if word is found. This also shows name of file
grep -l word file | xargs -r -i bash -c "echo {}:; cat {}"

Resources