This is my Linux script, I want to ask the user from outside what date is needed, then save that date and run the script for that date.
#/bin/bash
cDt=$(date +"%Y%m%d")
cd /home/dwh_landing/temp
echo 'Process_Date,Outfile,Outpath,Process_hour,Process_Minutes,Infile' > ccn_daily_${cDt}.csv
cd /home/dwh_landing/etl_scripts/etl_logs/
awk -F',' '{print $1 "," $2 "," $5}' *ccn-json*${cDt}* | grep 'creditControl.json' | awk -F '/' '{print $0 "," $5}' | awk 'match($0, /(\s\S*k)/ , a ) {print $0 "," a[1]}' >> /home/dwh_landing/temp/ccn_daily_${cDt}.csv
cd /home/dwh_landing/temp
cat ccn_daily_${cDt}.csv | wc -l >> ccn_daily_${cDt}.csv
now currently this script generate a csv for the current date files, I want to run this for a user request date, can I pass parameters from outside?
any help could be useful.
What do you mean by outside? Do you want to pass parameters when running the script? If so, you can run the script with
./myscript $(date +"%Y%m%d")
and use this argument with
#!/bin/bash
cDt="$1"
...
This is parameter number one, since the null parameter is the name of the script.
Also, you can validate a date string with
if ! date +"%Y%m%d" -d "$cDt" &> /dev/null; then
echo "$1 - invalid date string"
exit
fi
YOu can use a while loop to read a date off the user until the date is compliant (date returns a code 0) at which point we break from the loop and execute your script (in this case myscript), passing the read date variable dat as a parameter:
while true;
do read -p "Please enter a date in the format i.e. 20210201" dat;
if date -d "$dat" +"%Y%m%d";
then
break;
fi;
done
myscript "$dat"
Then in your script, amend the line:
cDt=$(date +"%Y%m%d")
to:
cDt="$1" # SEt cDt to the first passed parameter
Related
I have a students.txt (RollNo, Name, IDU, CGPA), If Roll number exists prompt the user to change the IDU and CGPA and update the same in the file named “Student.txt”
I made the following script:
#! /bin/bash
dispaly(){
awk -F ":" -v roll=$1 '{ if ( $1 == roll) {name = $2; print name; } }
END {if (name == "") print "not found" }' students.txt
}
echo "Enter the roll no."
read rno
if [ $rno -ge 1000 ] && [ $rno -le 9999 ]
then
dispaly $rno
# Now I have a valid $rno and want to update that line
else
echo Enter no between 1000 and 9999
fi
now I need help in taking user input for IDU and CGPA values and update the students.text file with that values against the record found.
In general "-" is used for standard input for awk e.g.
awk '{print($1)}' -
It's not clear to me exactly what you want here. Can't you use additional 'read' statements in the bash part of the script for input of the other 2 values?
first, I grep for roll
grep ^roll students.txt
if found then used awk to replace the records
awk -F : -v rno=$rno -v idu=$idu -v cgpa=$cgpa ' $1==rno { $3=idu;$4=cgpa} OFS=":" ' students.txt > tmp.txt && mv tmp.txt students.txt
I want to add some users who are in this file like:
a b
c d
e f
firstname lastname always
#!/bin/bash
Lines=$(cat newusers.txt | wc -l)
first=$(cat newusers.txt | awk '{print $1}')
last=$(cat newusers.txt | awk '{print $2}')
#test
echo $Lines;
echo $first;
echo $last;
until [ -z $1]; then
useradd - m -d /home/$1 -c "$1 + $2" $1
fi
before loop it works fine but I can't add newline.
The echo shows a c e and second for lastname b d f.
I tried to add newline in but it doesn't works.
What can i use for this?
Because I guess I can't add the user because of the newline problem.
I also searched on stackoverflow to find out a way to check if the user already exists by /dev/null but which variable do i have to use for it?
It's easier to process the file line by line:
while read first last ; do
useradd -m -d /home/"$first" -c "$fist + $last" "$first"
done < newusers.txt
I do not understand what you mean to do by your code, but if you want to read the file line by line and get the values of different fields then you can use the following code snippet:
#!/bin/bash
filename="newusers.txt"
while read -r line
do
fn=$( echo "$line" |cut -d" " -f1 )
ln=$( echo "$line" |cut -d" " -f2 )
echo "$fn $ln"
done < "$filename"
Note: You cannot add users the way you want to using bash script; since you will be prompted for password which must be supplied using tty you can use expect to program it; or use system calls.
i am trying to replace the second string by parameter 2.
the script takes the 2 parameters,then the script should check if the first parameter exist in file , if exist it should check in which line it is existing and it should replace only the second string in that file.
for ex: while running i am passing 2 parameters 1 and 2
./run.sh 1 2
the script should check if the parameter 1 exists if not it should write the parameter to file...now that is happening..
now if i pass the parameter 1 3 to script
the script should search where the parameter 1 is and replace 2nd string i.e 2 with 3..
How can i do this???
here is what i have tried
#!/bin/sh
#
FILE_PATH=/home/user/Desktop/script
FILE_NAME=$FILE_PATH/new.txt
echo $1
echo $2
param1=`cat $FILE_NAME | grep $1
if [ -z "$param1" ]
then
echo $1:$2 >> $FILE_NAME
else
param2=`cat $FILE_NAME | grep $1`
fi
the file which i am referring will have text like this
+abc.3434.res:192.168.2.34:5400
+efg.3123.co3:192.168.2.24:5440
+klm.gsdg.cm5:192.168.2.64:5403
if i pass parameter 1 as abc.3434.res and parameter 2 as 156.666.554.778
the script should replace
+abc.3434.res:192.168.2.34:5400 with
+abc.3434.res:156.666.554.778:5400
This will look for all lines in the format you describe, and with the first parameter matching, and replace the middle bits with the second parameter.
sed -i -e "s/\(+$1:\).*\(:.*\)/\1$2\2/" $FILENAME
You can use Awk:
awk -v f1="+$1" -v f2="$2" -F: '$1==f1 { $2=v2; s=1 }
1
END { if (!s) print f1 ":" f2 }' "$FILE_NAME"
Because Awk cannot access the shell's variables directly, we pass the values in with -v. The first condition matches when the first field is found; then the second field is changed, and s is set to 1 as a signal to ourselves that a substitution has taken place. The next line is unconditional, and simply prints all lines. At end of file, we add the new data if no match was found (s is zero).
use the following format:
sed s/olsstring/newstring/g
such as :
cat /etc/passwd |sed s/root/jeff/g
But if you want to store to file immedaitly, you should use -i:
sed -i s/olsstring/newstring/g yourfile
But grep and another command:
shell has a variable $? , it print return status
if you want to invoke for grep command, you should use :
grep -i blahblah |egrep -v egrep
if [$0 == 0 ];then
echo SUCCESS
fi;
I have 4 names (AA,BB,CC,DD) in a config file. This has been used in a script.
I am trying to get the output to be saved as
Source_Server_Path="/dev/FtpData_Files/START_STOP_"$1"_"$Current_Date""
where $1 will be any one in the names.
$Current_Date has to be in the format 05Jun2013.
$ foo="/bar/quux/$(date +%d%b%Y)"
$ echo "${foo}"
/bar/quux/07Jun2013
See also man date.
Sample script/method ... Try if it helps
config.txt
AA,BB,CC,DD
Script.sh
file=$1
config=`cat $file | awk -F "," '{print $1}'`
#It will take AA .. whatever parameter you pass
Current_Date=`date +%d%b%Y`
echo START_STOP_"$config"_"$Current_Date"
How to run
sh script.sh config.txt
Output
START_STOP_AA_07Jun2013
I am not a Linux scripting expert and I have exhausted my knowledge on this matter. Here is my situation.
I have a list of states passed as a command line argument to a shell script ( e.g "AL,AK,AS,AZ,AR,CA..." ). The Shell script needs to extract each of the state code and write it to a file ( states.txt) , with each state in one line. See below
AL
AK
AS
AZ
AR
CA
..
..
How can this be achieved using a linux shell script.
Thanks in advance.
Use tr:
echo "AL,AK,AS,AZ,AR,CA" | tr ',' '\n' > states.txt
echo "AL,AK,AS,AZ,AR,CA" | awk -F, '{for (i = 1; i <= NF; i++) print $i}';
Naive solution:
echo "AL,AK,AS,AZ,AR,CA" | sed 's/,/\n/g'
I think awk is the simplest solution, but you could try using cut in a loop.
Sample script (outputs to stdout, but you can just redirect it):
#!/bin/bash
# Check for input
if (( ${#1} == 0 )); then
echo No input data supplied
exit
fi
# Initialise first input
i=$1
# While $i still contains commas
while { echo $i| grep , > /dev/null; }; do
# Get first item of $i
j=`echo $i | cut -d ',' -f '1'`
# Shift off the first item of $i
i=`echo $i | cut --complement -d ',' -f '1'`
echo $j
done
# Display the last item
echo $i
Then you can just run it as ./script.sh "AL,AK,AS,AZ,AR,CA" > states.txt (assuming you save it as script.sh in the local directory and give it execute permission)