I have script to change the structure from a .txt file into a 'newusers' command readable structure, which is great for 1 file, but it means I have to edit said script everytime I need to use it with a new file. I've read about positional parameters, which I think it's what I need in this situation, but not sure how to apply that to achieve my goal.
What can I do to pass the arguments while executing the script so it works with every file without needing to edit the script further/each time?
So far my script works with bash script.sh, no arguments.
Code so far:
#!/bin/bash
clear
while IFS=: read -r userid username group; do
if [ "$group" = "group1" ]; then
groupid=2020
elif [ "$group" = "group2" ]; then
groupid=2040
else
echo "Unknown group"
fi
echo "$userid:password123::$groupid:$username:/home/$userid/bin/bash";
done #< users2.txt > newusers2.txt
Expected result:
bash script.sh oldfile1 newfile1
Cheers.
Related
How do I create a Bash script that takes a file name as input? Then, if that file exists, it should print "File exists"; if not, print "File does not exist".
For example, if I ran ./do-i-exist.sh ./do-i-exist.sh, the output should be only 'File exists'
file="$1"
read answer
if [ $file != -$2 ]
then
echo "File exists"
else
echo "File does not exist"
fi
This is what I'm working with but is not working for me, whenever I add an extension like .sh, .txt or something similar it won't find the file.
The test if a file exists can be done like this
if [ -f "$file" ]
then
This tests for a regular file, not for other kinds of files like a directory.
This is how you can do it. Pass the name of the file while like ./do-i-exist.sh file_path.
if [ -f "$1" ]
then
echo "File Exists"
else
echo "File does not exist"
fi
First of all, I want to thank anyone and everyone who tried to help. After 3 hard working days, I found the answer, here it is:
#!/bin/bash
file="$#"
if [ -f $file ]
then
echo "File exists"
else
echo "File does not exist"
fi
Using this table:
Variable Name
Description
$0
The name of the Bash script
$1 - $9
The first 9 arguments to the Bash script
$#
Number of arguments passed to the Bash script
$#
All arguments passed to the Bash script
$?
The exit status of the most recently run process
$$
The process ID of the current script
$USER
The username of the user running the script
$HOSTNAME
The hostname of the machine
$RANDOM
A random number
$LINENO
The current line number in the script
I and other users were focused on using $1 from my understanding this refers to the first argument passed to the script but for some reason, it wasn't working since it needed to pass more inputs.
As from my previous comments I didn't have control over the input. The input was hidden in a locked file, and I needed to feed my script to it.
From what we know $0 is only used to check for the file names, $1 to get the first statement and $# will just take anything(I guess).
I know absolutely nothing about bash and it was the first time ever using it, which is why it took me 3 days to solve this puzzle. This was part of a CTF and just like me, many others may struggle in the future to understand or know how to make a script that will just adapt to a series of inputs from a second script.
This is how it was supported to work:
I was given access to a very restricted server and on this server, I was given the encrypted-file.sh file. This file was supposed to be fed to /path/to/myfile.sh then encrypted-file.sh would execute a second command to open a third locked file hiding a flag on it.
This only works with the right bash file using the right variables on it for encrypted-file.sh to run without errors, which is what I accomplished here.
I used a while loop because it made sense in my case because I really needed a file for the script to work.
restore_file="$1"
while [ ! -f "$restore_file" ]
do
echo "File not found: $restore_file"
echo "Please provide a valid file:"
read restore_file
done
As written above, $1 is the first argument given to the script. In this case if no argument is given or that is not a file, it will prompt again.
By the way, use -d instead of -f to check for a directory.
I am trying to create a basic MapReduce function in bash (I am very new at it). I have two scripts at the moment, job_master.sh and map_function.sh. I am trying to run the map function from the job master to cut from a data file, and if it doesn't exist send it to key, but if it does send it to the file of that name. Nothing is happening when I run the job_master script or the map_function script on its own with a file as an argument. It was working before I added the if statements into the map_function.
I have included both codes below if anyone is able to spot why they are not running. I tried including echo statements to test and it is not entering the loop in the job_master script, or doing anything at all in the map_function script.
MAP_FUNCTION
#!/bin/bash
while IFS="," read -r date prod remainder; do
if [ ! -e "$prod" ];
then
echo $prod >> keys
else
echo $prod >> $prod
fi
done
JOB_MASTER
#!/bin/bash
files=$(ls | egrep 'sales_a*')
for elem in $files ; do
./map_function.sh $elem
done
The map function script is waiting for input. Look at this:
while IFS="," read -r date prod remainder; do
# ...
done
Where will read get its input? It's waiting on stdin, but you're not passing it anything.
On the other hand, I see that you're calling the map function script like this:
./map_function.sh $elem
That is, you are passing a command line argument. But the map function script doesn't use that argument. It seems you want to redirect the content of $elem to the stdin of the script.
Write like this:
for elem in sales_a*; do
./map_function.sh < "$elem"
done
This also fixes some other issues you had in the script. files=$(ls | grep 'sales_a*') looks buggy, and it's an inappropriate way to iterate over files.
I'm totally new in writing shell scripts so I could use some help here.
I would like to write a script that when run with no parameters it just echo backs, and when it is given a data (.dat) file it displays the content of it.
Excuse me for my bad English,
R.
This script, when run with no parameters it just echo backs and when a filename is passed as argument, it displays the content of it:
#!/bin/sh
# Explanation - We use'$#' to count number of arguments.
if ! [ $# -gt 0 ]; then
# Explanation - Zeroth argument '$0' is scriptname itself. Print it.
cat "$0"
else
# Explanation - Print (cat) 1st argument.
cat "$1"
fi
NOTE: Since you've used 'minix' tag, I tested it on minix3. The script works well on minix as well as linux.
I am trying to get the entire output of a bash script to save to a file. I currently have one argument (ip address) at the beginning of the code looks like this:
#!/bin/bash
USAGE="Usage: $0 [<IP address>]"
if [ "$#" == "0" ]; then
echo "$USAGE"
exit 1
fi
ip_addr=$1
What I'd like to do is add another argument called "output", that the entire output of the script will save to. I'm aware I could just run myscript.sh | tee textfile.txt, but I'd like to make the script a little easier to run for others.
Thanks in advance,
hcaw
After the usage message, add the following line:
exec > "$2"
This will redirect standard output for the rest of the script to the file named in the 2nd argument.
Then run using
myscript 192.0.2.42 output.txt
Recently i got an assignment at school, where we are to write a small program in Bash Scripting Language.
This shell script is supposed to accept some Positional Parameters or Arguments on the command line.
Now, with the help of an if-else statement i check if the argument is present, if the argument is present the script does what it is supposed to do, but if the argument is not present - i display an error message, prompting the user to input an argument and pass the argument again to the same shell script...
Now, i want to know if this approach is good or bad in Bash Programming paradigm. I'm slightly suspicious that this might run too many tasks in the background that are kept open or that are never ended and keep on consuming memory... please help.
Here's a small snippet (assume the script name to be script1.bash):
#!/bin/bash
if [ $# -gt 0 ]; then
read -p "Please enter your name: " name
script1.bash $name
elif [ $# -eq 1 ]; then
echo "Your name is $1!"
fi
It's ... questionable :-)
The main problem is that you're spawning a subshell everytime someone refuses to input their name.
You would be better of with something like:
#!/bin/bash
name=$1
while [[ -z "$name" ]] ; do
read -p "Please enter your name: " name
done
echo "Your name is $name!"
which spawns no subshells.